r_duck
r_duck

Reputation: 149

Is it possible to write statements after return?

Is the return statement the last statement inside main or is it possible to write statements after return?

#include <iostream>

using namespace std;

int main() {

    cout << "Hello" << endl;

    return 0;

    cout << "Bye" << endl;
}

This program compiles but only displays "Hello".

Upvotes: 2

Views: 2606

Answers (5)

wally
wally

Reputation: 11012

is it possible to write statements after return?

It is possible and valid to write more statements after the return. With gcc and Clang, I don't get a warning, even with the -Wall switch. But Visual Studio does produce warning C4702: unreachable code for this program.


The return statement terminates the current function, be it main or another function.

Even though it is valid to write, if the code after the return is unreachable the compiler may eliminate it from the program as per the as-if rule.


You could have the return statement executed conditionally and you could have multiple return statements. For example:

int main() {
    bool all_printed{false};
    cout << "Hello" << endl;
    if (all_printed) 
        return 0;
    cout << "Bye" << endl;
    all_printed = true;
    if (all_printed) 
        return 0;
}

Or, you could use a goto before and after the return and some labels, to execute the return statement after the second output:

int main() {

    cout << "Hello" << endl;
    goto print;

return_here:
    return 0;

print:
    cout << "Bye" << endl;
    goto return_here;
}

Prints:

Hello
Bye

Another solution, linked to in this answer, would be to use RAII to print after the return:

struct Bye {
    ~Bye(){ cout << "Bye" << endl; } // destructor will print
};

int main() {
    Bye bye;
    cout << "Hello" << endl;
    return 0; // ~Bye() is called
}

Upvotes: 4

Antonio
Antonio

Reputation: 20296

You can write statements after a return like in your example, but they will never be executed, and some compilers will give you a warning "unreachable code" (For example C4702 in VS2015).

To execute code after a return statement, see Is it possible to execute code after return statement in C++?

Upvotes: 1

Red.Wave
Red.Wave

Reputation: 4235

Such code is usually generated in the intermediates debugging stages while narrowing the window to capture a bug. It works but must be fixed by the programmer asap.

Upvotes: 0

Dark Matter
Dark Matter

Reputation: 317

The only way you could usefully have code after a "return" is if the "return" statement is conditional.

if (error) return 0;
# All code invoked past this point knows error is false

This isn't great coding style, you're creating code where there are 2+ ways to exit. When you need to debug it you'll find it more challenging to put in flags and "what happened when we exited" and so forth.

Upvotes: -1

R Sahu
R Sahu

Reputation: 206637

is return statement the last statement inside main or is it possible to write statements after return?

The run time behavior of executing a return statement is independent of the function. Be it main or some other other function, when a return statement is executed, nothing after that is executed in the function.

It is possible to write statements after the return statement. They are superfluous since they are not executed. A smart compiler might be able to even omit creation of object code corresponding to the statements after the return statement.

Upvotes: 1

Related Questions