Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #89 Handle invalid argument

You've detected that the integer value of argument x passed to the current function is invalid. Write the idiomatic way to abort the function execution and signal the problem.

enum {
    E_OK,
    E_OUT_OF_RANGE
};

int square(int x, int *result) {
    if (x > 1073741823) {
        return E_OUT_OF_RANGE;
    }
    *result = x*x;
    return E_OK;
}

New implementation...
< >
programming-idioms.org