In C++, is "return;" the same thing as "return NULL;"?

19,297

Solution 1

is return; the same as return 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 as return 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.

Solution 2

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).

Solution 3

return, return 0 and return NULL are not the same.

return is only valid with functions returning void, e.g.:

void func(...);

return 0 is for functions returning some int type, e.g.:

unsigned long func(...);

although it works with floating point types as well:

double func(...) { ... return 0; }

return NULL is for functions returning some pointer type, e.g.:

Something *func(...);
Share:
19,297
Derek W
Author by

Derek W

Software Engineer. I'm just here to learn and also to share what I've learned.

Updated on June 08, 2022

Comments

  • Derek W
    Derek W almost 2 years

    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!