问题
In a test, one of my classmates wrote the following function, to flip a number:
int tukor(int n)
{
int k=0;
while(n!=0)
{
k=k*10+n%10;
n=n/10;
}
n=k;
}
You will notice a complete lack of any return statements, but when cout<<tukor(1234);
is run (namespaaace std
is used), it outputs 4321
. Now the entire class is confused as to how this is possible, even after the teacher added the lines n=0;n=12;
at the end of the function. It has worked for all test cases so far.
Is this caused by undefined behavior or something similar?
EDIT: changing k before the n=k statement changes the return value, and if it is removed, the return value is 0.
来源:https://stackoverflow.com/questions/50346184/what-does-a-c-in-function-return-without-a-return-statement-return