P
pozz
A very simple question. What do you use as the return value of a
function for error/ok results?
I can't decide between 0 for error and !=0 for ok, or 0 for ok and !=0
for errors.
If !=0 is ok, I can write:
if (myfunction()) printf("OK\n");
but I have only one error code (0), while, in some cases, it could be
useful to have more informations about the type of error.
If !=0 is error I can have several error codes, but I have to write
if (!myfuction()) printf("OK\n");
that apparently seems myfunction has not terminated with success.
Another strategy is to define custom error code:
#define ERROR_OK 0
#define ERROR_UNDEFINED 1
#define ERROR_WRONGVALUE 2
and so on, and always write:
if (myfuction() == ERROR_OK) printf("OK\n");
function for error/ok results?
I can't decide between 0 for error and !=0 for ok, or 0 for ok and !=0
for errors.
If !=0 is ok, I can write:
if (myfunction()) printf("OK\n");
but I have only one error code (0), while, in some cases, it could be
useful to have more informations about the type of error.
If !=0 is error I can have several error codes, but I have to write
if (!myfuction()) printf("OK\n");
that apparently seems myfunction has not terminated with success.
Another strategy is to define custom error code:
#define ERROR_OK 0
#define ERROR_UNDEFINED 1
#define ERROR_WRONGVALUE 2
and so on, and always write:
if (myfuction() == ERROR_OK) printf("OK\n");