Function with missing return value, behavior at runtime-Collection of common programming errors

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?

Originally posted 2013-11-09 20:53:37.