D
D
Dmitry2015-06-23 14:09:17
Programming
Dmitry, 2015-06-23 14:09:17

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

5 answer(s)
D
Denis Ineshin, 2015-06-23
@Thelema

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;
    }
}

S
Sergey, 2015-06-23
@zenden2k

For example, if you rely on NRVO (Named Return Value Optimization) in C++, then it's better to use a single return.

K
Konstantin Berkov, 2015-06-23
@konstantin_berkow

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.

D
Dimexide, 2015-06-23
@Dimexide

Well, depending on what kind of function, and so, of course you can.

M
Mrrl, 2015-06-23
@Mrl

The multiple return, as well as the break, continue, switch and throw statements, are nothing more than disguises for the goto statement, which has been fought over for the past few decades. So, if you are not afraid of goto, then use it to your health.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question