What's the return value from a function call if that function doesn't really provide one

怎甘沉沦 提交于 2019-11-26 18:33:48

问题


Let's say we have following code:

int func(char str[], int len) {
    // Don't return anything here.
}

int main() {
    char str[] = "Hello";
    int result = func(str, strlen(str));
    printf("%d\n", result);
}

It will print some string value -1679929632 on my computer. And it changes from time to time when I execute.

Can anyone explain why this happen?


回答1:


If no return statement appears in a function definition, control automatically returns to the calling function after the last statement of the called function is executed. In this case, the return value of the called function is undefined. If a return value is not required, declare the function to have void return type; otherwise, the default return type is int.

As mentioned above its undefined so finding root cause behind some random value as return will be useless.



来源:https://stackoverflow.com/questions/28060029/whats-the-return-value-from-a-function-call-if-that-function-doesnt-really-pro

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