I'm learning C++. The codes below confused me:
int test_return(int a)
{
    for (int i = 40; i < 44; i++)
    {
        if (i == a)
        {
            cout << "return here with i: " << i << endl;
            return 59;
        }
    }
}
int main()
{
    cout << "in main: " << test_return(61) << endl;
    return 0;
}
I know I'm missing a return statement at the end of function test_return.
But the compiler says no error and it works when executed.
So I pick up some particular numbers like 40,44,59,61 to see which one the function test_return will choose to return.
I tried several times, the output is always this:
in main: 44
It seems like that the function test_return returned the int i before the for-statement ended.
My question is:
Is this legal?
How does it work?
Update:
I add these codes at the end of function test_return:
int i = 100;
int square = i * i;
It comes out:
in main: 10000
Thanks for @Vlad from Moscow's answer! It helps.
 
     
    