What does a c++ in function return without a return statement return? [duplicate]

家住魔仙堡 提交于 2021-01-28 06:18:28

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!