[C/C++] classifing function return value type…

In terms of function’s return value, I classify three types.

type 1 : Function may fail with quite high possibility.
return value tells details about success or fail reason.

return_value == 0 : general success.
return_value > 0 : success with additional information [value represents additional information]
return_value < 0 : fails [value represents error code]

int open_service(...) {...}

type 2 : Function may fail with very low possibility.
return value tells the result or “success or fail”.

return_value == NULL : fails.
otherwise : return_value is result.

Item* List::next() {...}
FILE* fopen(...) {...}

In this case, we may need additional function like “get_last_error()” – like MSVC’s API.

type 3 : Function never fails.
return value is result, ‘void’ or instance itself to easy-next-use.

int get_id() {...}
void set_color(int color) {...}
Paint* Paint::set_color(int color) {...; return this;}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s