I'm writing a CSV parsing library in C and am considering whether to express error codes as return values or as parameters passed by reference.
For example, here are the signatures for the function that counts the number of columns in a CSV file, using both designs:
// Method one
// Return value: number of columns
size_t countfields(
unsigned char *data, // First/header row in the file
size_t len, // Length of the buffer
errno_t *err // Error code (0 == success)
);
// Method two
// Return value: error code (0 == success)
errno_t countfields(
unsigned char *data, // First/header row in the file
size_t len, // Length of the data buffer
size_t *numfields // Number of columns
);
Obviously, this decision only applies to languages like C which don't support exceptions so the only way to test for an error is by examining designated error-holding variables after a function call.
Which method should I use: returning the error code, or using a parameter, and why?