在 c 中,返回值如何在 if 语句中添加返回值的函数中工作?

乔吉伊

快速提问:

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章