7

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!

Derek W
  • 9,708
  • 5
  • 58
  • 67
  • 8
    You seem to confuse a lot of things... – Niklas B. Nov 03 '12 at 19:55
  • `NULL` is not 'nothing', it is a C macro that expands to something like `0` (but not necessarily) that is of pointer type and doesn't alias any valid pointers. The C++ equivalent is `nullptr`. – Will Nov 03 '12 at 20:00
  • In particular, the null pointer is a valid value of a pointer type. – Niklas B. Nov 03 '12 at 20:01
  • From the Null tag on the site: "Null means *nothing* or *unknown* (depending on context)." – Derek W Nov 03 '12 at 20:03
  • NULL is not 'nothing' in C++. It's just a macro (defined in ``) that evaluates to number `0`. But afaik it's not a part of the language itself and it has no meaning in C++. – v154c1 Nov 03 '12 at 20:04

6 Answers6

13

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 2
    +1 The advice to use `0` instead of `NULL` [comes from Stroustroup](http://www.stroustrup.com/bs_faq2.html#null), but that's not all he said about the issue: he also mentioned using `nullptr` in C++11 :) – Sergey Kalinichenko Nov 03 '12 at 20:05
  • 1
    Thank you for the comprehensive breakdown. I apologize as I had much confused, but your answer really cleared up a lot of things. – Derek W Nov 03 '12 at 20:12
  • 3
    @dasblinkenlight: Not everything Stroustroup says must be correct! Don't forget he's responsible for C++ in the first place :P – Lightness Races in Orbit Nov 03 '12 at 20:22
  • 1
    @LightnessRacesinOrbit I wish the site had a way to nominate a "comment of the day" :):):) – Sergey Kalinichenko Nov 03 '12 at 20:56
  • `possibly with a void* type` is false. From [4.10p1]: `A null pointer constant is an integral constant expression (5.19) prvalue of **integer type** that evaluates to zero` – Jesse Good Nov 04 '12 at 00:02
  • @JesseGood: That doesn't say anything about `NULL`. `NULL` may be equivalent to a null pointer constant or it may simply be convertible to one. – Lightness Races in Orbit Nov 04 '12 at 00:26
  • 1
    @JesseGood: Ah, 18.1/4 identifies that `NULL` must _be_ a null pointer constant and that therefore the same must apply for `NULL`. A footnote identifies this exact scenario. Okay then :) – Lightness Races in Orbit Nov 04 '12 at 00:27
7

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

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 6
    See §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)." – Joseph Mansfield Nov 03 '12 at 19:57
  • Your answer brings up possibly the largest oversight that I made. What would happen if my function was actually returning something? In retrospect, I can't believe that I did account for this scenario. Also sftrabbit, thank your citation. – Derek W Nov 03 '12 at 20:16
1

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(...);
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
0

It works like a void function, where the return will simply exit the function and return nothing at all. This has been asked before.

Community
  • 1
  • 1
Matt Olan
  • 1,911
  • 1
  • 18
  • 27
0

There is a difference in the resulting machine code:

return will just put the instruction pointer back to where the caller came from.

return 0 or return NULL will put 0 on the stack, and then put the instruction pointer back to where the caller came from. (In C, NULL is usually mapped to 0, although that can (may?) differ in different implementations. In C++, NULL is not supposed to be used).

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
0
const testNullParity = () => {
 return null 
}

const testNullParity2 = () => {
 return 
}


console.log(testNullParity()) //null
console.log(testNullParity2())  //undefined

in javascript, return is short for return undefined as null and undefined are two different types, but they are loosely equal ie. testNullParity() == testNullParity2() is true but not testNullParity() === testNullParity2()