简体   繁体   中英

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

return; in a void function. What does it actually do?

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

In the example you've shown, the return does nothing. 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.

(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

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.)

The return statement when encountered causes the function to return immediately instead of continuing to run any code that comes after it.

If the function returns a value, the return statement must specify a value to return. For a function with a void return type, no value is needed.

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.

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.

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 ... .

If x is not NULL then the program will execute what is on else statement and also ... some other code here ...

During my time in programming in C++, I learned that ' void functions ' have no return-type . 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. However, when we use the void function, things change. The return-type , 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.

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. 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.

I hope that helped you!

For further reading you can visit: http://www.cs.fsu.edu/~cop3014p/lectures/ch7/index.html

Return statement in void method

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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