Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function with missing return value, behavior at runtime

Tags:

c++

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?

like image 928
nabulke Avatar asked Apr 08 '10 06:04

nabulke


People also ask

What does a function return when it is missing a return statement?

If no return statement appears in a function definition, control automatically returns to the calling function after the last statement of the called function is executed. In this case, the return value of the called function is undefined.

What happens if you forget the return statement in a non void function?

If control reaches the closing curly brace ( } ) of a non- void function without evaluating a return statement, using the return value of the function call is undefined behavior.

What happens if you call a function that has a return value but you don't save the return value in a variable?

nothing, the return value gets ignored.

What does non void function does not return a value?

It means that there is no default return value for your function outside of the for loop.


4 Answers

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.

like image 117
alseether Avatar answered Oct 05 '22 02:10

alseether


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.

like image 22
piotr Avatar answered Oct 05 '22 01:10

piotr


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

like image 22
Muxecoid Avatar answered Oct 05 '22 01:10

Muxecoid


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

like image 23
codaddict Avatar answered Oct 05 '22 00:10

codaddict