2

I have the following code in c++:

    int fff ( int a , int b )
{
   if (a>b )
      return 0;
   else a+b ; 
}

although I didn't write 'return' after else it does not make error ! < br/> in main() when I wrote:

cout<<fff(1,2);

it printed 1 ? How did that happened
can any one Explain that ?

Mu_Nizar
  • 33
  • 4
  • 2
    Unfortunately, not all control paths returning value is not an error in C++ . See this for explanation http://stackoverflow.com/questions/1735038/why-not-all-control-paths-return-a-value-is-warning-and-not-an-error – Naveen Aug 25 '10 at 05:16
  • You might have got a warning similar to `Not all control paths return a value` or something like that... – liaK Aug 25 '10 at 05:17
  • if you are using gnu compiler, try using -Wall to see the warnings. – tristan Aug 25 '10 at 05:31

4 Answers4

5

This what is called undefined behavior. Anything can happen.

C++ does not require you to always return a value at the end of a function, because it's possible to write code that never gets there:

int fff ( int a , int b )
{
   if (a>b )
      return 0;
   else return a+b;

   // still no return at end of function
   // syntactically, just as bad as original example
   // semantically, nothing bad can happen
}

However, the compiler cannot determine if you never get to the end of the function, and the most it can do is give a warning. It's up to you to avoid falling off the end without a return.

And if you do, you might get a random value, or you might crash.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
2

$6.6.3/2- "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."

A compiler may or may not diagnose such a condition.

Here

else a + b;

is treated as an expression without any side effect.

Chubsdad
  • 24,777
  • 4
  • 73
  • 129
0

the "random" return vaule is determined by the CPU register value after the call, since the register is 1 after the call, so the value is 1.

If you change you code, the function will return diffrent value.

zhongshu
  • 7,748
  • 8
  • 41
  • 54
0

A good compiler (e.g. gcc) will issue a warning if you make such a mistake, and have a command line switch to return a non-zero error status if any warnings were encountered. This is undefined behaviour: the result you're seeing is whatever value happened to be in the place that the compiler would normally expect a function returning int to use: for example, the accumulator register or some spot on the stack. Your code doesn't copy a+b into that location, so whatever was last put in there will be seen instead. Still, you're not even guaranteed to get a result - some compiler/architecture might do something that can crash the machine if the function didn't have a return statement: for example - pop() a value from the stack on the assumption that return has pushed one - future uses of the stack (including reading function-return addresses) could then get results from the memory address above or below the intended one.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252