简体   繁体   English

函数什么时候应该返回什么?

[英]When should a function return what?

I'm a bit confused about the return type of a function, specifically when to return what. 我对函数的返回类型有些困惑,特别是什么时候返回什么。 If we have any arbitrary function, let's say min(x,y), which should return according to the if statements, what should I return outside of their scope, which is required by the function's declaration. 如果我们有任意函数,比如说min(x,y),它应该根据if语句返回,那么在函数声明所要求的范围之外,我应该返回什么呢? I've learned that it is common to use "return 0" or "return 1" but I don't understand why that is, and why it can't just return either of the if's return statements. 我已经知道使用“ return 0”或“ return 1”是很常见的,但是我不明白为什么会这样,为什么它不能只返回两个if的return语句。

// compute difference largest - smallest
int   diff(x, y)
{
   if (x > y)
    return x - y;
   if (y > x)
    return y - x;
   if (x == y)
    return 0;
   return 1;
}
  • If X is bigger than Y , return XY (obvious) 如果X大于Y ,则返回XY (明显)
  • If Y is bigger than X , return YX (also obvious) 如果Y大于X ,则返回YX (也很明显)
  • If X and Y are equal , what do you think should be returned? 如果XY 相等 ,您认为应该返回什么?
    If they're equal, both XY and YX are 0 , so it doesn't really matter, does it? 如果它们相等,则XYYX均为0 ,所以并不重要,对吗?

Your final clause could be written any of: 您的最终条款可以写成以下任何一种形式:

return x-y;
return y-x;
return 0; 

Because they two values are equal, they will ALL evaluate to 0 . 因为两个值相等,所以它们的所有值都将为0

The important part is to make sure the contract of the function is maintained. 重要的部分是确保功能合约保持不变。
According to the comments, the function claims it will return the difference between the two values. 根据评论,该函数声称它将返回两个值之间的差。 So it should return the difference in all cases , whether the bigger value is X, Y, or they are equal. 因此,无论较大的值是X,Y还是相等,它都应返回所有情况下的差。

The name of your function is extremely misleading. 函数的名称极具误导性。 Because it is named min , it implies it will return the minimum value, not the difference. 因为它名为min ,这意味着它将返回最小值 ,而不是差值。 You should change the name of the function to something like diff or delta for clarity. 为了清楚起见,应将函数名称更改为diffdelta类的名称。


Ultimately, the simpler way to write the function would be: 最终,编写函数的更简单方法是:

// compute largest - smallest
int diff(int x, int y)
{
   if (x > y)
    return x - y;   // Handles X is bigger than Y
   else
    return y - x;   // Handles Y bigger than X, *or* X and Y are the same.
}

Finally, some very short versions that I would consider better. 最后,一些我认为更好的简短版本。

int diff(int x, int y) { return x>y? x-y:y-x; }
int diff(int x, int y) { return abs(x-y);     }

The compiler needs to make sure that you return something. 编译器需要确保您返回了某些内容。 If you have returns inside of if statements, such as you have, how is the compiler going to tell if you have covered all cases? 如果您在if语句中有返回值(例如您有),那么编译器将如何判断您是否已涵盖所有情况?

Even if you do something like the code below, how is the compiler going to know that you will return in all circumstances? 即使您执行类似以下代码的操作,编译器如何知道在所有情况下都将返回? Because of that this code will throw an error: 因此,此代码将引发错误:

int min(int x, int y)
{
    if (x > y)
        return x - y;
    if (y > x)
        return y - x;
    if (x==y)
        return 1;
}

How can you combat this? 您如何应对呢? You can write another return at the end of the function, as you have. 您可以根据需要在函数结尾处编写另一个返回。 Now the compiler know that you will return a value no matter what happens. 现在,编译器知道无论发生什么情况您都将返回一个值。

However there are better ways to make sure that you will always return a value. 但是,有更好的方法来确保您将始终返回值。 For example, you can use an else statement as a catch-all: 例如,您可以使用else语句作为包罗万象的内容:

int min(int x, int y)
{
    if (x > y)
        return x - y;
    if (y > x)
        return y - x;
    else
        return 1;
}

Because the else is guaranteed to catch all cases, the compiler is happy about it, and will not throw any errors. 因为保证else能够捕获所有情况,所以编译器对此感到满意,并且不会引发任何错误。

For a function that's computing the difference between two values, you would only return 1 if the difference between those two values was 1, 0 if they were equal. 对于计算两个值之间的差的函数,如果两个值之间的差为1,则仅返回1;如果两个值相等,则返回0。

There's a school of programming that says it's better to have a single return at the end of the function, rather than multiple returns scattered throughout. 有一所编程学院说,最好在函数末尾有一个返回,而不是分散在整个过程中的多个返回。 Instead of returning the difference in each branch, compute and store that difference, and return it at the end of the function: 无需在每个分支中返回差异,而是计算并存储该差异,然后在函数末尾将其返回:

int diff( int x, int y );
{
  int delta = 0;

  if ( x >= y )
    delta = x - y;
  else 
    delta = y - x;

  return delta;
}

Like most programming rules, there are exceptions. 像大多数编程规则一样,也有例外。 There are times where you want to exit immediately if a certain condition isn't met. 在某些情况下,如果不满足特定条件,您立即退出。 Knowing when which approach is the better one is a matter of experience. 知道哪种方法更好是一个经验问题。

The return type of the function is specified in the definition, int in your case. 函数的返回类型在定义中指定,在您的情况下为int Actually you need to specify the type of input arguments ( x, y ) and then the type of the output is usually derived from their type - like 2/3 cases in your function and the last one is integer ( 0 ). 实际上,您需要指定输入参数的类型( x, y ),然后通常从其类型派生输出的类型-例如函数中的2/3种情况,最后一个是整数( 0 )。

Remark: You also need to choose better name for the function. 备注:您还需要为函数选择更好的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM