18

I'm pretty proficient in PHP, but I've started dabbling with C. I've seen the code

return 0;

at the end of functions that don't return a value. This isn't used in PHP, because if a function is doesn't have a return, a value NULL is automatically returned.

All I'm asking is, in simple English, what does the return 0 actually do? Is it like PHP, where it returns its argument as the value of the function call? Is it just good practice?

I know this question has been asked many times before, but I'm asking it from the point of view of a PHP developer. The answers google throws up haven't been that concise.

unor
  • 92,415
  • 26
  • 211
  • 360
Starkers
  • 10,273
  • 21
  • 95
  • 158

7 Answers7

22

Is it like php, where it returns its argument as the value of the function call? Is it just good practise?

Yes, PHP and many other languages borrowed the return keyword from 'C'. And in all the languages, the return keyword has the same function - to return from the function. Anything that follows return keyword is the value that is returned to the caller.

Is it a good practise? Yes and No. Not all functions should return a value. And quite a few in the standard library even, do not return any value. Hence their return type is void.

But main function should return 0(also EXIT_SUCCESS) to identify that the program has executed successfully. And -1 otherwise (also EXIT_FAILURE)

EDIT: (Thanks to @KeithThompson):

EXIT_FAILURE is implementation defined. 1 is a common value of EXIT_FAILURE but the whole point is, you need not know.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
9

For historic reasons, it is possible to write return 0; to return from a function that has been declared as void, like so:

void foo( /* arguments */ )
{
  /* do things */
  return 0;
}

This does nothing, and the 0 (or whatever you put there) is thrown away. Also, sensible compilers will give you a warning message if you do this. So don't do this.

Brooks Moses
  • 9,267
  • 2
  • 33
  • 57
  • This is a constraint violation in Standard C. (So it's only "possible" in the sense that anything is possible) – M.M Nov 05 '19 at 00:46
7

Functions in C return int by default, if no other return type is defined. return 0 would be good practice to make sure the function returns a known value, as opposed to some random value, in case the caller is looking at the return value.

John
  • 131
  • 5
  • 6
    Worth noting that this is ANSI C, and modern C (C99 and newer) discourage or disallow inferred types. – Jonathan Grynspan Feb 18 '13 at 02:24
  • this is the case with c89 only – Aniket Inge Feb 18 '13 at 02:27
  • Is it really good practice to encourage people to inspect the return value of a function that has been declared as not returning a value? – aroth Feb 18 '13 at 03:35
  • 1
    The meaning of *"Functions in C return int by default,"* is that in K&R and c90 it was OK with the compiler to write `foo(){return 1;}` for a function taking an arbitrary number of arguments of unspecified type and returning `int`. No, it is not good practice to rely on that behavior in writing new code, but the compiler needs to support it because there is legacy code out there. – dmckee --- ex-moderator kitten Feb 18 '13 at 04:22
6

It is literally returning an int of 0.

rocky
  • 3,521
  • 1
  • 23
  • 31
3

The C programming language allows programs exiting or returning from the main function to signal success or failure by returning an integer, or returning the macros EXIT_SUCCESS and EXIT_FAILURE. On Unix these are equal to 0 and 1 respectively. A C program may also use the exit() function specifying the integer status or exit macro as the first parameter.

Apart from the macros EXIT_SUCCESS and EXIT_FAILURE, the C standard does not define the meaning of return codes. Rules for the use of return codes vary on different platforms.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
2

In C you don't have to return a value only if you declare a function with void at the start of it. First example:

#include <stdio.h>
 int main()
{
   printf("Hello World!");
    return 0; // you have to use return because main starting with int
}

Second Example:

#include <stdio.h>
void main()
{
printf("Hello World!");
//in this case return is useless, main is a void function

}
1

return 0 literally returns EXIT_SUCCESS. Even if you do not type in return 0 it will automatically return 0 for you. Check out this (Search for The return value of main) link for more information.


A quick snapshot from that link:

enter image description here