my question is return;
the same as return NULL;
in C++?
I understand that in C++, return NULL;
is the same as return 0;
in the context of pointers. Obviously for integers, this is not the case as NULL cannot be added, subtracted, etc. And that it is encouraged by some to use 0 instead of NULL for pointers because it is more convenient for portability. I'm curious if this is another instance where an equivalence occurs.
I suspect that they are equivalent because return;
is saying return 'nothing' and NULL is 'nothing.' However, if someone can either confirm or deny this (with explanation, of course), I would be very grateful!
There is no difference in assigning zero or NULL to a pointer variable. NULL may be just more readable. When you are returning zero, you are really returning zero. If a function signature has void specified as a return value, it means that the function does not return a value.
Returning null is often a violation of the fail fast programming principle. The null can appear due to some issue in the application. The issue can even go to production if the developer has not implemented proper exception handling, which can help quickly detect the issue.
return value of main() is generally used as exit status of the program, and 0 is just a value which by convention is considered to represent sucessful execution.
Several alternatives for returning null values include using null object reference types, a null object pattern, and a result type as the return type. Therefore, the recommendation is to return an empty value instead of a null to keep the code clean and error-free.
is
return;
the same asreturn NULL;
in C++?
No.
return
is used to "break" out from a function that has no return value, i.e. a return type of void
.
return NULL
returns the value NULL
, and the return type of the function it's found in must be compatible with NULL
.
I understand that in C++,
return NULL;
is the same asreturn 0;
in the context of pointers.
Sort of. NULL
may not be equivalent to 0
, but it will at least convert to something that is.
Obviously for integers, this is not the case as NULL cannot be added, subtracted, etc.
You can perform addition and subtraction to pointers just fine. However, NULL
must have integral type (4.10/1 and 18.1/4 in C++03) anyway so it's moot. NULL
may very well be a macro that expands to 0
or 0UL
.
Some modern compilers will at least warn you if it was actually NULL
you wrote, though.
And that it is encouraged by some to use 0 instead of NULL for pointers because it is more convenient for portability. I'm curious if this is another instance where an equivalence occurs.
No. And I disagree with this advice. Though I can see where it's coming from, since NULL
's exact definition varies across implementations, using NULL
will make it much easier to replace with nullptr
when you switch to C++11, and if nothing else is self-documenting.
return
with no expression works only if your function is declared void
, in a constructor, or in a destructor. If you try to return nothing from a function that returns an int
, a double
, etc., your program will not compile:
error: return-statement with no value, in function returning ‘int’
According to §6.6.3/2 of C++11:
A return statement with neither an expression nor a braced-init-list can be used only in functions that do not return a value, that is, a function with the return type void, a constructor (12.1), or a destructor (12.4).
(thanks sftrabbit for the excellent comment).
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