1

Consider the following piece of code:

#include <iostream>
using namespace std;

int testReturn()
{
     // no return
}

int main() 
{
    cout << "!!!Hello World!!!" << testReturn() << endl;
    return 0;
}

The compiler warns: ..\src\test.cpp:15:1: warning: no return statement in function returning non-void [-Wreturn-type]. So in my compiler, the output is 1:

!!!Hello World!!!1

Is no return statement specified as unspecified behaviour? Or is it always non zero?

halfer
  • 19,824
  • 17
  • 99
  • 186
Łukasz Przeniosło
  • 2,725
  • 5
  • 38
  • 74
  • 3
    1. pick a laungage (in this case the answer is the same; but not true in general) 2. Undefined is undefined. – UKMonkey Dec 12 '17 at 13:51
  • 2
    It's *undefined* behavior. – Some programmer dude Dec 12 '17 at 13:51
  • It is undefined behavior, anything can happen. C11 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.` A `return` statement prevents this from happening. – Lundin Dec 12 '17 at 13:56
  • 1
    Your code don't compile in C, please don't tag both C and C++ language for no reason. – Stargateur Dec 12 '17 at 13:59
  • The accepted answer of the duplicated says Standard c++ Section 6.6.3. in the actual Standard it is in Section 9.6.3 **The return statement** – alseether Dec 12 '17 at 14:02

2 Answers2

6

(Question was originally (mis-)tagged as C too).

The behaviour of your program is undefined in both C and C++.

The only int function that has an implicit return value is main, and 0 is implied in that case; again in both C and C++.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 3
    "The only int function that has an implicit return value is `main`" Only in C++. In C there are no implicit return values. – sepp2k Dec 12 '17 at 13:53
  • @sepp2k: You sure? I thought from C99 you could. This appears to confirm https://stackoverflow.com/questions/4138649/why-is-return-0-optional – Bathsheba Dec 12 '17 at 13:54
  • 2
    @sepp2k C11 (N1570) 5.1.2.2.3: "reaching the `}` that terminates the `main` function returns a value of 0" – Angew is no longer proud of SO Dec 12 '17 at 13:56
  • I was, but apparently that doesn't mean that I'm right ;-) I guess it should be "C++ and C99 and later" then. – sepp2k Dec 12 '17 at 13:57
  • @sepp2k C99 5.1.2.2.3 `reaching the } that terminates the main function returns a value of 0`. – Lundin Dec 12 '17 at 13:57
  • The only real difference I know of between C and C++ when it comes to `main` is that in the former you can call `main` from itself. In C++ that's undefined. – Bathsheba Dec 12 '17 at 13:58
0

The likely explanation is that a function returning int always return something, and it's usually the content of a register. It probably happens that the register used for return value is in your case the same used to compute the last expression before returning from the function (on x86 targets, certainly eax).

For x86 at least, the return value of this function should be in eax register. Anything that was there will be considered to be the return value by the caller.

Because eax is used as return register, it is often used as "scratch" register by calee, because it does not need to be preserved. This means that it's very possible that it will be used as any of local variables. Because both of them are equal at the end, it's more probable that the correct value will be left in eax.

From the '89 standard as quoted in the new testament:

Flowing off the end of a function is equivalent to a return with no expression. In either case, the return value is undefined.

That standard usually expresses the on-the-ground behavior of pre-existing implementations

Selvam
  • 109
  • 1
  • 14