-1

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

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
harshit batra
  • 75
  • 1
  • 13

3 Answers3

5

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.

Joe
  • 410
  • 3
  • 13
0

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.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
-1

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.

Andreas DM
  • 10,685
  • 6
  • 35
  • 62