Answer the question
In order to leave comments, you need to log in
Is it correct to use multiple "return" statements in a function?
Is it correct to use multiple "return" statements in a function?
Answer the question
In order to leave comments, you need to log in
If there are many conditions that lead to return, then there can be switch ... case and its ability to fall through several conditions, like this:
function test (a) {
switch (a) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
return;
case 7:
// other code
break;
}
}
For example, if you rely on NRVO (Named Return Value Optimization) in C++, then it's better to use a single return.
It depends on the code-style adopted in the command, for example, I think that using more than two returns everywhere except equals is not very good.
Well, depending on what kind of function, and so, of course you can.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question