Zeuthen: Writing a C library, part 1
Posted Jun 29, 2011 7:16 UTC (Wed) by
gowen (guest, #23914)
In reply to:
Zeuthen: Writing a C library, part 1 by cmccabe
Parent article:
Zeuthen: Writing a C library, part 1
Returning error codes is a great convention because you can flag them with __attribute__((warn_unused)). Then the programmer will get a warning from the compiler unless he checks the return code.
Returning error codes is fine in certain circumstances (particularly for functions where the side-effects are the point). Sometimes, though, you want your functions to be functions in the lambda-calculus sense - you want to return *results*.
In general, you can't return both results *and* error codes. (As I said, for pointers you can return NULL, and for functions whose domain of valid results is limited in some sense [abs()], you can, but if you're returning anything other than a pointer, int-type or floating-point-type, you're basically hosed.
(
Log in to post comments)