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 ?
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:
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.