Function with missing return value, behavior at runtime

Posted by nabulke on Stack Overflow See other posts from Stack Overflow or by nabulke
Published on 2010-04-08T06:54:02Z Indexed on 2010/04/08 7:03 UTC
Read the original article Hit count: 333

Filed under:

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?

© Stack Overflow or respective owner

Related posts about c++