What is the difference between return
, return 0
and return NULL
statement? Which return
should I use at the end of a void function?

- 19,179
- 10
- 84
- 156

- 75
- 1
- 13
-
On a void function you can only `return;` – DimChtz Mar 27 '16 at 10:14
-
In C++ you can actually do: `return void();` for void functions... Which I think is fun, but just `return;` is what's expected. – Alexis Wilke Jul 08 '23 at 17:15
3 Answers
You can't return NULL in a void function,because NULL is defined by #define NULL 0
in C++,(return 0 or NULL means that you return a value that is int or other type) void function means that it have no return value,you can write code:
return;
to exit a void function.

- 410
- 3
- 13
NULL
is a macro defined by a number of standard library headers, including <stddef.h>
.
It is intended to represent a null-pointer value. It may be defined as numerical literal 0, or as nullptr
. In the first case it can be used as an integer type value, in the second case it cannot. So it's best to only use it as a nullpointer value.
However, in modern C++ prefer to write nullptr
, which is a built-in.

- 142,714
- 15
- 209
- 331
return;
returns nothing (void
)
return 0;
terminates program 0
indicating success
return NULL;
It may be defined as ((void*)0)
in C, or 0
or 0L
in C++, nullptr
C++11 depending on the implementation.

- 10,685
- 6
- 35
- 62
-
It doesn't depend on the compiler.In C++, NULL is defined by `#define NULL 0` in header file. – Joe Mar 27 '16 at 10:26
-
2@Joe: It does depend on the implementation. And there are many more possibilities: any integral type literal 0, or `nullptr`. – Cheers and hth. - Alf Mar 27 '16 at 10:31