-1

"C++ Primer" states that failing to provide a return after a loop that contains a return is an error.

Is that true? What I know is once the return statement is executed, the control will jump over the rest of the function back to the calling function.

So, what is the need of that return outside of the loop?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Mason
  • 501
  • 3
  • 11
  • 1
    In case the loop completes without the `return` being executed. – 001 Oct 21 '20 at 20:18
  • C++ Primer 5th Edition - 6.3.2 – Mason Oct 21 '20 at 20:19
  • I can imagine code that would not need a return after the loop, if the loop was infinite for instance. So I think that's a piece of generally good advice rather than a hard and fast rule. Basically all possible control paths through a non-void function should return a value. – john Oct 21 '20 at 20:21
  • 1
    please post the full quote. I can imagine that the book refers to loops that eg not necessarily have more than zero iterations, one that has a break somewhere or other control flow – 463035818_is_not_an_ai Oct 21 '20 at 20:21
  • 1
    I can't resist plugging this question, with my accepted answer: https://stackoverflow.com/questions/46467407/does-this-function-have-explicit-return-values-on-all-control-paths/46467470#46467470 – Bathsheba Oct 21 '20 at 20:38
  • After the loop, put a `throw std::logic_error("should never happen");` and you ought to be good. – Eljay Oct 21 '20 at 20:56

1 Answers1

3

If the function has a non-void return type, you need a return statement at EVERY possible non-exception exit point in the function's flow of execution. That means having a return statement after the loop, in case the loop ends without hitting its inner return, eg:

void loop1()
{
    for(...)
    {
        if (condition)
            return; // <-- function exits here
    }
    // <-- if reach here, return is optional...
}

int loop2()
{
    for(...)
    {
        if (condition)
            return ...; // <-- function exits here
    }
    return ...; // <-- if reach here, return is required...
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • What if I know for sure that the inner ```return``` statement is gonna be executed? Is it still an error not to provide one after the loop? – Mason Oct 21 '20 at 20:22
  • Well, in a bizarre code-design world, you *could* avoid that final `return` statement by having a `throw` statement, instead. – Adrian Mole Oct 21 '20 at 20:23
  • @Mason That's probably one of the reasons, such is reported only as a warning by the compiler. – πάντα ῥεῖ Oct 21 '20 at 20:23
  • @Mason It is fine to not have a trailing `return` if the function always returns from inside the loop. – François Andrieux Oct 21 '20 at 20:24
  • 1
    @Mason when return type is not `void` then every path has to lead to a `return`, whether loops, or other control flow – 463035818_is_not_an_ai Oct 21 '20 at 20:24
  • 1
    @AdrianMole It is not that odd. For example an `at` member function could very do that if the loop searches for an element and the function specifies that it throws if it fails to find. – François Andrieux Oct 21 '20 at 20:24
  • @Mason YOU may know the inner `return` is ALWAYS hit, but the COMPILER usually doesn't. So not having an outer `return` is a warning, not an error (unless you have configured the compiler to treat warnings as errors). In this case, I would design the loop to `break` instead of `return` and then let the outer code `return` what it needs. – Remy Lebeau Oct 21 '20 at 20:25
  • From a formal perspective, falling off the end of a function that returns a value ends up with undefined behavior. I’d you add a `return` statement to silence a warning (missing return statement) might have to remove it if you change compilers (warning: unreachable code). – Pete Becker Oct 21 '20 at 20:58