12

As expected, the compiler (VisualStudio 2008) will give a warning

warning C4715: 'doSomethingWith' : not all control paths return a value

when compiling the following code:

int doSomethingWith(int value)
{
    int returnValue = 3;
    bool condition = false;

    if(condition)
        // returnValue += value; // DOH

    return returnValue;
}

int main(int argc, char* argv[])
{
    int foo = 10;
    int result = doSomethingWith(foo);
    return 0;
}

But the program runs just fine. The return value of function doSomethingWith() is 0.

Is is just undefined behavior, or is there a certain rule how the result value is created/computed at runtime. What happens with non-POD datatypes as return value?

nabulke
  • 11,025
  • 13
  • 65
  • 114
  • 1
    Have a look to this answer: http://stackoverflow.com/questions/1610030/why-can-you-return-from-a-non-void-function-without-returning-a-value-without-pro/1610454#1610454 – Fernando N. Apr 08 '10 at 07:48
  • @fnieto: Ah, I see, this is a very good answer. Didn't find it myself though, before asking... – nabulke Apr 08 '10 at 07:52

4 Answers4

17

It is Undefined behaviour as specified in the ISO C++ standard section 6.6.3:

Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.

piotr
  • 5,657
  • 1
  • 35
  • 60
  • 2
    Specific wordings:— Flowing off the end of a function is equivalent to a `return` with no value; this results in undefined behavior in a value-returning function. – kennytm Apr 08 '10 at 07:02
  • 1
    'main' function is an exception to that rule. In nabulke code, last 'main' function line is redundant. – Fernando N. Apr 08 '10 at 07:46
  • As an _important_ addition to this answer, 3.6.1/5 states: "If control reaches the end of `main` without encountering a `return` statement, the effect is that of executing `return 0;`". – Lightness Races in Orbit Jul 15 '11 at 14:22
  • @LightnessRacesinOrbit: it is still an odd practice to omit it even in the main function, especially where it is unclear whether or not it is intentionally omitted. – László Papp Dec 25 '14 at 21:48
  • @lpapp: I can see why you may argue that, though I disagree: omitting `return 0` from `main` is extremely common and usually very clearly not a mistake. Returning anything _but_ `0` at the very end of the outermost scope of `main` would be the odd practice!! – Lightness Races in Orbit Dec 25 '14 at 23:18
7

For x86 processors, the standard calling convention puts the return value to the EAX register. Practically it means that for most compilers if we reach the end of the function without returning, the result of the last math operation will be returned. However, you can not rely on it and it is not portable.

http://en.wikipedia.org/wiki/X86_calling_conventions#cdecl

Melebius
  • 6,183
  • 4
  • 39
  • 52
Muxecoid
  • 1,193
  • 1
  • 9
  • 22
3

Updating @piotr answer.

From the C++17 Standard Section 9.6.3

Flowing off the end of a constructor, a destructor, or a function with a cv void return type is equivalent to a return with no operand. Otherwise, flowing off the end of a function other than main (6.6.1) results in undefined behavior.

alseether
  • 1,889
  • 2
  • 24
  • 39
2

Not returning a value from a value-returning function leads to undefined behavior.

codaddict
  • 445,704
  • 82
  • 492
  • 529