Q:

C++ function returning string

#include <iostream>

using namespace std;

string test(string s)
{
    string rv = "Hey Hello";
    if(s=="")
        return rv;
    else
        cout<<"Not returning"<<endl;

}

int main()
{
    string ss = test("test");
    cout<<ss<<endl;
}

The above code should not return any value and probably print garbage, but its returning "Hey Hello" even without return statement at the end in test function.
Can you tell my why its behaving like this?

A:

reaching the end of a function returning non-void without any return statement is 
undefined behavior
you should have compiler warning for this kind of things if they are active
compiler are allowed to assume that undefined behavior is unreachable so the compiler have probably deleted the if statement and kept only the branch that doesn't lead to undefined behavior

