-3

I don't really understand why we need to use return 0; at the end of a function. I sorta understand why we need it at the end of main() (To signify the end of the program?) but I really don't understand why we also need it at the end of a regular function.

Cognitive Hazard
  • 1,072
  • 10
  • 25
Jordan Baron
  • 11
  • 1
  • 1
  • 1
    it depends - its just convention to signify that there was no error – Daniel A. White Nov 27 '16 at 20:04
  • You need to return a value at the end of `main` if you want the calling process to know the status of the program it executed. For example, a Windows batch file can take action depending on the value returned by the program, with `errorlevel`. – Weather Vane Nov 27 '16 at 20:08
  • 1
    `main` is the only function that *doesn't* need a `return` statement (as of C99). – dreamlax Nov 27 '16 at 20:11
  • 1
    @dreamlax void functions also don't need `return` statement, or am I wrong? – Honza Dejdar Nov 27 '16 at 20:23
  • 1
    @HonzaDejdar: Yes was more pointing out that OP mentioned he understands why one is needed for `main` but doesn't understand why it's needed for all other functions, and I was saying that out of the functions OP is enquiring about, `main` is the one function that is guaranteed not to need one. – dreamlax Nov 27 '16 at 20:27
  • In C no function *needs* a return statement ! Even in a non-void function, the return statement can be omitted so long as the caller never uses the return value. – M.M Nov 27 '16 at 20:28
  • 1
    Why are folks down-voting this question? OP is clearly a novice; one should expect novices to ask questions in novice terms. – Cognitive Hazard Nov 27 '16 at 23:19
  • @Honza Quite true, but you can write `return;` to end a void function without further executing the next statement. – Spade 000 Sep 17 '20 at 12:56

5 Answers5

3

If, in your code, you say that your function returns a value, but you don't actually return one:

int my_function(int x)
{
    // bad practice!
}

... that is an error bad practice. You said that you were going to return a value (in this case, an 'int') but you did not.

Similarly, if there are multiple paths through your function:

int my_function(int x)
{
    if (x == 3)
        return 42;  // good

    // bad!
}

and you've again declared the intent to return a value, but some paths through your function do not, that is also an error bad practice; you have broken your promise to the callers of your function.

All paths through a function should end up at a return statement:

int my_function(int x)
{
    if (x == 3)
        return 42;  // good

    return 1;  // good
}

If you don't want to return any values, then declare the function void:

void my_function(int x)
{
    // good
}

UPDATE: above, you may notice that I replaced "an error" with "bad practice". That's because the word 'error' means something very particular within the C standard, and forgetting a return statement doesn't meet that definition (instead, it's considered to be "undefined behavior," and your compiler might not even complain.) However, you as a human being should consider it an error in your code, and avoid forgetting return statements on all exit paths out of your functions.

carloswm85
  • 1,396
  • 13
  • 23
Cognitive Hazard
  • 1,072
  • 10
  • 25
  • It's not an error in C to omit the return statement. It is only an error if you both omit the return statement, and try to use the return value in the calling code. (And the nature of this error is undefined behaviour with no diagnostic required -- compilers don't have to warn about this, although some do). – M.M Nov 27 '16 at 20:30
  • 1
    While your comment is correct, it seems that OP is a novice, and I felt those details were too much for him/her at such an early stage. My answer is proscriptive, towards good programming practice. I can change my words 'error' to 'bad' if you like – Cognitive Hazard Nov 27 '16 at 20:35
2

You do not need to return 0 in every function. For example:

void my_function(int *p_data)
{
    *p_data = 5;
}

int main(void)
{
    int data = 0;
    my_function(&data);
    printf("Data: %d", data);

    return 0;
}

This will work perfectly fine.

aeb-dev
  • 313
  • 5
  • 16
0

You don't if you declare the return type as void

Delete Me
  • 102
  • 2
0

If a function is declared as returning a type other than void, then it must have a return statement. The only exception to this is the main function, which as of C99, can omit the return statement (when it is omitted, the behaviour is the same as if there was a return 0; statement before the closing } of main).

Consider this erroneous test function:

int test(int a)
{
    if (a == 0)
        return 123;
}

int main()
{
    int x;
    x = test(0); // x equals 123
    x = test(1); // x equals ???
}

If you call test with 0 as the argument, the function will give you back 123, but what if you call it with something other than 0? What would you expect the result to be? There is no “default return value” for functions in C, a return value must be given if the return type is not void (with the one exception being the main function).

dreamlax
  • 93,976
  • 29
  • 161
  • 209
0

Debugging

For debugging in linux, You usually return 0 when the program executed correctly, the return code can be obtained by executing echo $? in the terminal. You can create your own status code like:

int func(){
   if (1) return 9; //true
   return 0;
int main (){
   return func();
}
$ gcc t.c
$ ./a.out
$ echo $?
9
$

What is echo $? in linux terminal?

Spade 000
  • 109
  • 1
  • 11