Is a return statement mandatory for C++ functions that do not return void?

§6.6.3/2:

Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.

So it depends on your definition of mandatory. Do you have to? No. But if you want your program to have well-defined behavior, yes.*

*main is an exception, see §3.6.1/5. If control reaches the end of main without a return, it will have the effect of return 0;.

Leave a Comment