0

i found this code in some site that it doesn't have any return statement for returning the value. But in the site it is written as it gives a compile time error. While i was trying to compile the program for knowing what the error will be seen in compiler. The program compiled successfully with out giving any error..

The c++ code is :

int multiply(int x, int y)
{
  int product = x * y;
}

int main()
{
  cout << multiply(4, 5) << endl;
  return 0;
}

output : 20

Now I am confused that without any return statement how the compilation is successful.

Cœur
  • 37,241
  • 25
  • 195
  • 267
shaan
  • 96
  • 9
  • 1
    Compile two versions, one with, the other without, the return statement. Run both in a debugger and look at the disassembly before each `ret`. What is happening will be obvious. That said, don't rely on it. – WhozCraig Jun 06 '14 at 04:54
  • enable all warnings... – user1810087 Jun 06 '14 at 04:54
  • Whatever said it has to be a compiler error was wrong. Perhaps you misinterpreted it and it was only talking about a specific compiler (e.g., MSVC). – chris Jun 06 '14 at 04:55
  • Hi chris i am using GnuGcc compiler and the code has compiled in code block IDE..:) – shaan Jun 06 '14 at 05:03
  • @chris: MSVC emits an `error C4716: 'multiply' : must return a value` diagnostic even if I turn the warning level all the way down (/W0). – Blastfurnace Jun 06 '14 at 05:06

3 Answers3

2

Without return statement in function this code invoked undefined behavior. A non void function must have a return statement.

C++11 § 6.6.3 The return statement:

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.

As WhozCraig stated in comment, there is one standard-defined exception to the non-void return value requiring a return: int main()

C++11 § 3.6.1p5 [basic.start.main]

If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;

In case of UB you may get either expected or unexpected result.
The printed value could be the value stored in the stack.

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
  • Hi, haccks i do believe that A non void function must have a return statement.But in the above code the function executed successfully without any return statement for which i am confused...:) – shaan Jun 06 '14 at 04:59
  • 2
    @user1903535, Read again: **undefined behaviour**. – chris Jun 06 '14 at 04:59
  • 1
    Note: there is one standard-defined exception to the non-void return value requiring a `return`: `int main()` C++11 § 3.6.1p5 [basic.start.main] "If control reaches the end of main without encountering a return statement, the effect is that of executing `return 0;`" To the best of my knowledge, its the only one that has this exception. All other non-void non-return-stmt functions are ill-formed. Further, having a return statement isn't enough. All paths must have a *reachable* return statement. – WhozCraig Jun 06 '14 at 05:02
  • It is only undefined behaviour if you actually reach the end of the function. It's OK to not have a return statement at the end if the function is never called, or it has an infinite loop, or a different return statement earlier on that is always reached – M.M Jun 06 '14 at 05:17
1

It's undefined behavior if a function that returns non-void doesn't return anything. The compiler should give you at least a warning.

What probably happens is, the value of product is still in the stack after the function exits, and std::cout prints that value. But again, don't rely on undefined behavior.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • Or (for most x86 calling conventions) the calculation happens to put its result in the `eax` register, which is where the caller expects the return value. – nobody Jun 06 '14 at 05:07
1

It's undefined behavior, so there's no guarantee that it will work.

When it does work correctly, it's because the result happens to be computed in the same CPU register that is used to return integer values from functions. This is dependent on a many factors (e.g. compiler version, system ABI, compiler options, surrounding code), so you can't rely on it.

Russell Borogove
  • 18,516
  • 4
  • 43
  • 50