Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are common approaches to meaningful function return values?

Tags:

c

return-value

I know EXIT_SUCCESS/EXIT_FAILURE is typically used for main() exit to indicate the successfulness of a program.

But is it also used (commonly or suggested) for normal function returns? I'm trying to write some "standard" code so wondering if I should use them instead of 0 or -1..

like image 904
Figo Avatar asked Dec 03 '09 23:12

Figo


People also ask

How do you return a value from a function?

To return a value from a function, you must include a return statement, followed by the value to be returned, before the function's end statement. If you do not include a return statement or if you do not specify a value after the keyword return, the value returned by the function is unpredictable.

What type of value can a function return in JavaScript?

JavaScript functions can return a single value. To return multiple values from a function, you can pack the return values as elements of an array or as properties of an object.

In what situation does a function return a value?

If a function is defined as having a return type of void , it should not return a value. In C++, a function which is defined as having a return type of void , or is a constructor or destructor, must not return a value. If a function is defined as having a return type other than void , it should return a value.

Does a function always return a value?

Yes it always returns a value, if explicit one is not given, it returns undefined .


2 Answers

Do no use EXIT_SUCCESS or EXIT_FAILURE for functions, or for anything other than exit calls for that matter.

Feel free, however, to define your own types for your functions to return, but that depends on the situation and the exact code you're writing.

Some common approaches are:

  • Functions returning pointers almost always return NULL (which is 0 according to ANSI C, but NULL is a more descriptive name to use) in case of errors
  • Functions that return indexes or positions usually return -1 on error (because it can't be a real index, while 0 can, so 0 isn't a good error return value)
  • In more complex cases, you may find it useful to define a new return type with an enum, one of which is your error, and check for that.

As long as you are consistent, don't stray away from the conventions too far, and document everything, you should be OK.

like image 155
Eli Bendersky Avatar answered Oct 13 '22 22:10

Eli Bendersky


You should never use EXIT_SUCCESS or EXIT_FAILURE outside of the context of a call to exit() or the return value to main(). The values of these integers are not specified by the standard, so you cannot write portable code if you assume that they are 0 or 1 respectively.

The standard does specify that exit(0) and a return 0 from main() behave the same way as exit(EXIT_SUCCESS) and return EXIT_SUCCESS, but these values are "special" in that they are to be interpreted in an implementation defined manner. That is, the actual value passed to the system may not be 0, and the constant value of EXIT_SUCCESS is free to not be 0 if the implementation desires, so long as return 0 from main and return EXIT_SUCCESS from main behave the same way.

If you use these values in your functions, your callers will have to compare against EXIT_SUCCESS directly to determine if the function succeeded, which personally speaking I would find very inconvenient.

int MyFunction1 () { /* ... */ return EXIT_SUCCESS; }
int MyFunction2 () { /* ... */ return 0; }

// Portable:
if (MyFunction1 () == EXIT_SUCCESS) { ... }
if (MyFunction2 () == 0) { ... }
if (!MyFunction2 ()) { ... }

// Not portable:
if (MyFunction1 () == 0) { ... }
if (!MyFunction1 ()) { ... }
if (MyFunction2 () == EXIT_SUCCESS) { ... }

The problem becomes more obvious with EXIT_FAILURE:

int MyFunction1 () { /*... */ return EXIT_FAILURE; }
// Not portable
if (MyFunction1 ()) { ... }
if (MyFunction1 () == 1) { ... }
// The only way to make this portable.
if (MyFunction1 () == EXIT_FAILURE) { ... }
like image 37
Dan Olson Avatar answered Oct 13 '22 23:10

Dan Olson