快速提问:
int x;
int y;
int function (int a, int b)
{
if (x == y)
{if (y == 1) {return -1;}
else {return 1;}}
else {return 1;}
}
为什么上面的代码要求我在 if-else 语句之外添加一个返回值,我应该返回什么?
我相信这是一个通用的编译器警告return
,并且没有考虑到条件案例内部的案例。
所以,这就是编译器“看到”的:
int function (int a, int b)
{
if condition
{...}
else {...}
}
...并且编译器在问“回报在哪里?”。
您可以简单地return 0; // not needed, only here for avoiding compiler warnings
在函数代码的末尾添加它应该没问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句