AlwaysLearning
AlwaysLearning

Reputation: 201

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

My Herb Schildt book on C++ says: "... In C++, if a function is declared as returning a value, it must return a value." However, if I write a function with a non-void return type and do not return anything, the compiler issues a warning instead of an error: "Control reaches end of non-void function."

I use gcc (MinGW) and have set the -pedantic flag.

Upvotes: 20

Views: 13552

Answers (7)

daveangel
daveangel

Reputation: 573

Like the GMan said the only exception is the main function. I still see tons of books returning 0 in main which isn't really necessary. Oh well I guess it could be worse and you could be learning from a book that uses void main() instead of int main(). But I think what you should learn from all this is that your compiler is complaining for a reason and it's good you took note of it since it will usually save you headaches in the long run.

Upvotes: 0

AnT stands with Russia
AnT stands with Russia

Reputation: 320381

Forgetting to include a return statement in some control path of a value-returning function does not make your code ill-formed. I.e. you should normally expect the code to compile (maybe with a warning). In that sense it is not "mandatory".

However, actually flowing off the end of value-returning function in C++ is always undefined behavior. (In C it is undefined behavior only if the calling code actually uses the result.)

Upvotes: 1

CB Bailey
CB Bailey

Reputation: 791451

It's not mandatory to have a return statement in a function declared as returning non-void and it doesn't have to lead to undefined behaviour.

Such a function could:

  • Not return, say by entering an infinite loop
  • Return by throwing an exception
  • Call a function that itself does not return, such as std::terminate

Of course, if a function avoids undefined behaviour by always doing one of the above it probably shouldn't be declared as returning non-void if possible.

One obvious case where it would need to is if it is a virtual function which for a particular point in a class hierarchy can't return a valid value and always exits via an exception.

Upvotes: 6

liori
liori

Reputation: 42237

It is mandatory--it is an undefined behavior when such function ends without returning anything (so compilers might actually implement some kind of special behaviour). However, there are some special cases.

::main is an exception, it is assumed that return 0; is at the end of its code.

Also, you don't have to return a value in a function that does not return cleanly, f.e.:

int Foo() {
    throw 42;
}

Upvotes: 11

GManNickG
GManNickG

Reputation: 503795

§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;.

Upvotes: 27

cmjreyes
cmjreyes

Reputation: 256

Is it mandatory? I don't believe so, however not returning a value in a non-void returning function is undefined as per my understanding of the c++ standards (except for main, which returns 0).

Does that mean it's OK? Probably not - if the function is supposed to return a value, you should be returning one, that could get real messy in complex code bases.

Upvotes: 1

Tom
Tom

Reputation: 45104

Yes, it must return a value.

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.

This question will bring more light to the subject

Upvotes: 4

Related Questions