Reputation: 10551
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.
Upvotes: 18
Views: 122392
Reputation: 316
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:
Upvotes: 1
Reputation: 310
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
}
Upvotes: 2
Reputation: 71
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.
Upvotes: 3
Reputation: 25725
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.
Upvotes: 22
Reputation: 9527
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.
Upvotes: 9
Reputation: 131
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.
Upvotes: 7