3

I asked this question before and I understood many things. I suspect that something similar is happening here, so I want to be sure about it. I have this simple program who adds 2 numbers.

#include <stdio.h>

int addFunc(int, int);

int main()
{ 
    int sum;
    int num1=1, num2=2;

    sum = addFunc(num1,num2);     //function call 

    printf("\nsum = %d\n\n", sum); 
    return 0; 
} 

int addFunc(int a,int b)        //function declarator
{ 
    int add; 
    add = a + b;

    return add;             //return statement of function.
}

Since this function isn't void there is a return statement in the function. If I omit the return value, save it and compile it, I don't get any errors from the compiler (nor warnings). And when I run it it gives me a correct result.

But how does the program know which value to return, since I don't specify any? Does C return the last calculated variable in the function?

Community
  • 1
  • 1
yaylitzis
  • 5,354
  • 17
  • 62
  • 107
  • 1
    The returned value is undefined. – Maroun Nov 20 '13 at 10:12
  • I guess the question would then actually be _why his program compiles and still returns the right result_? – penelope Nov 20 '13 at 10:14
  • If you enable your compiler's warning features then I bet you do get a warning. – ams Nov 20 '13 at 10:30
  • 2
    Because undefined behavior means that _anything_ could happen. "Anything" includes: the program crashing & burning, the program working correctly, the program seemingly working correctly but crashing on some conditions, the program doing completely random things etc etc. – Lundin Nov 20 '13 at 10:30
  • 3
    Compile it with `gcc -Wall`. It gives `warning: control reaches end of non-void function [-Wreturn-type]` Even better if you also add `-Werror` flag. – anishsane Nov 20 '13 at 10:32
  • @ams Reasons why a compiler might not warn is that the program is not undefined if callers do not use the result of a function without `return e;`, and that the program is not undefined if the function loops indefinitely without execution ever reaching the final `}`. Both behaviors are legal and are more or less impossible to identify soundly and completely in the general case. A compiler writer may use this as excuse not to detect even the easy cases. – Pascal Cuoq Nov 20 '13 at 10:34
  • @PascalCuoq If the compiler does not warn on *this* program it's because warnings are disabled. As far as I'm aware, whether callers use the result is not significant, especially for non-static functions. GCC, at least, requires `-Wall` (or something more specific) to warn about this case. – ams Nov 20 '13 at 10:47
  • 1
    @ams “As far as I'm aware, whether callers use the result is not significant” See the accepted answer. The program `int f(void) { int x = 1; x++; } int main(void) { f(); }` is a valid C99 program. – Pascal Cuoq Nov 20 '13 at 10:59
  • @PascalCuoq That clause requires that a program's behaviour remains "defined" when the caller does not use the undefined returned value. It is also not an error to omit the return statement. It does not say that this is a good thing to do, and it does not say that the compiler should not emit a warning, in this case. In the general, non-trivial case, the definedness of a program that omits a return statement from a non-static function is unknowable. Similarly for a static function whose address has been taken. – ams Nov 20 '13 at 11:15

3 Answers3

7

Is it necessary the return command in a non-void function?

Yes. This is necessary to return a value from a non-void function.

Omitting the return statement from your function but assigning it to sum in main will invokes undefined behavior. In this case sometimes you may get the result you expected and sometimes what I expect and sometimes what compiler expects!! Sometimes it may get crashed also.
As Pascal Couq mentioned in his comment:

6.9.1 Function definitions:

12: If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    +1 The relevant clause in the C99 standard is 6.9.1:12 “If the `}` that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined” (Note that if execution reaches any `return e;`, even as final statement but not necessarily so, it means that the final `}` is not reached.) – Pascal Cuoq Nov 20 '13 at 10:30
  • Many Thanks @PascalCuoq for giving standard reference. I searched for the standard but I did't find this quote. – haccks Nov 20 '13 at 10:39
7

That is working just by coincidence.

Most likely, the value of a+b is stored in EAX register. Generally EAX register is also used to store return value from function.

As in main actual return value is available in EAX which is again being used to pass value to printf function. Hence you are getting the expected output.

Rohan
  • 52,392
  • 12
  • 90
  • 87
  • 1
    +1 I like this answer better because it explain *how* it could happen to work. – ams Nov 20 '13 at 10:49
0

You didn't mention the compiler you are using or the machine you are compiling this for. But in general, the compiler will give an error if you omit the return statement in a function which return a value. This is implementation dependent since the standard mentions this is undefined behavior

As far as the value returned goes, this depends on the calling convention. Each architecture defines it's own set of registers used in function calls (where values are returned, for passing parameters and so on...). So for you, it's simply a matter of coincidence the result ends up in the correct register. It might be the behavior of the compiler you are using. If you try a different compiler you may get a different result.

Pandrei
  • 4,843
  • 3
  • 27
  • 44