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..
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.
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.
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.
Yes it always returns a value, if explicit one is not given, it returns undefined .
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:
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.
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) { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With