简体   繁体   English

为什么我们使用回报; void函数中的语句?

[英]Why do we use return; statement in a void function?

return; in a void function. void函数中。 What does it actually do? 它实际上做了什么?

void function() {
    if(x==NULL) {
        return;
    }
    else{
        /////
    }
}

In the example you've shown, the return does nothing. 在您显示的示例中, return不执行任何操作。 But think about this example: 但想想这个例子:

void do_the_thing()
{
    if(it's already done)
        return;

    do the thing;
}

If doing the thing is expensive, or causes problems if it's been done already, you'd like logic like this. 如果做的事情很昂贵,或者如果它已经完成就会引起问题,你会喜欢这样的逻辑。 And, besides "expense", there are plenty of reasons a function might choose to do less than its full repertoire of actions during any given call. 并且,除了“费用”之外,在任何给定的呼叫期间,功能可能选择比其完整的动作所做的更少的原因。

In other words, returning from a void function, although it won't return a value, will at least keep the function from doing any of its later stuff. 换句话说,从void函数返回,虽然它不会返回值,但至少会使函数不再执行任何后续的操作。

(Now, with that said, having a return in the middle of the function isn't the only way of achieving such a thing. Instead of if statements that cause the function to return early, you could use if statements or else clauses to cause the function to only do things if it needs to. Returning to my example, you could also write (现在,有了这样说,在函数中间return并不是实现这种事情的唯一方法。而不是if语句导致函数提前返回,你可以使用if语句或else子句来引起如果需要的话,只做事情的功能。回到我的例子,你也可以写

void do_the_thing()
{
    if( ! already done) {
       do the thing;
    }
}

And this leads to an eternal style debate, namely, whether it's a good or bad idea to have multiple return statements sprinkled through a function, or if there should always be exactly one, at the end.) 这导致了一种永恒的风格辩论,即,通过函数散布多个return语句是否是好的或坏的想法,或者最后是否应该总是只有一个。)

The return statement when encountered causes the function to return immediately instead of continuing to run any code that comes after it. 遇到return语句会导致函数立即返回,而不是继续运行它之后的任何代码。

If the function returns a value, the return statement must specify a value to return. 如果函数返回值,则return语句必须指定要返回的值。 For a function with a void return type, no value is needed. 对于具有void返回类型的函数,不需要任何值。

Generally, this is done to short-circuit the execution, meaning that the remaining code in the function won't be executed (the else statement wouldn't be evaluated or anything else in the function. 通常,这样做是为了使执行短路,这意味着函数中的剩余代码将不会被执行(else语句将不会被评估或函数中的任何其他内容。

This helps from the performance aspect, but also from a defensive programming standpoint as you are setting exactly where the execution returns instead of assuming it will get to the end of the function correctly even though it doesn't need to (code could be accidentally added or changed and affect the expected behavior) 这有助于从性能方面,但从防御性编程的角度来看,因为您正在设置执行返回的确切位置而不是假设它将正确地到达函数的末尾,即使它不需要(代码可能被意外添加)或改变并影响预期的行为)

它导致函数(不是程序,只是该函数)终止而不做任何进一步的操作。

You use return to stop the function without running all the code. 您使用return来停止该函数而不运行所有代码。

Using your example: 使用你的例子:

void function(){
  if(x==NULL){
     return;
  }
  else{
       /////
  }
  ... some other code here ...
}

If x is NULL then the function is stopping and doesn't run ... some other code here ... . 如果xNULL那么函数正在停止并且不运行... some other code here ...

If x is not NULL then the program will execute what is on else statement and also ... some other code here ... 如果x not NULL那么程序将执行else语句上的else以及... some other code here ...

During my time in programming in C++, I learned that ' void functions ' have no return-type . 在我用C ++编程的过程中,我了解到' void functions '没有返回类型 A return-type is used to declare functions, like int main() where the type int is a return type and the function has to return an integer value to imply successful termination. 返回类型用于声明函数,如int main() ,其中int类型是返回类型,函数必须返回一个整数值以暗示成功终止。 However, when we use the void function, things change. 但是,当我们使用void函数时,事情会发生变化。 The return-type , void ; 返回类型void ; as the name suggests, means that it will not return a value, it will not return anything as it is void - empty. 顾名思义,意味着它不会返回一个值,它不会返回任何内容,因为它是无效的 - 空的。 Hence, if you try to return a value from a method ( function ) that is declared void, you will get a compiler error. 因此,如果您尝试从声明为void的方法( 函数 )返回值,则会出现编译器错误。

But if like you suggested: 但如果像你建议的那样:

void function() {    
if(x==NULL) {
    return;
}
else{
    /////
}

} }

The above code will also, once again give an error because the void method is not built to return values. 上面的代码也将再次给出错误,因为void方法不是为了返回值而构建的。 If the function is of return-type int only then will this retrun command work, but a return statement cannot be used in a void function as it gives an error. 如果函数只是return-type int则此retrun命令将起作用,但是返回语句不能在void函数中使用,因为它会给出错误。

I hope that helped you! 我希望能帮到你!

For further reading you can visit: http://www.cs.fsu.edu/~cop3014p/lectures/ch7/index.html 如需进一步阅读,请访问: http//www.cs.fsu.edu/~cop3014p/lectures/ch7/index.html

Return statement in void method void方法中的return语句

https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

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

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