tags:

views:

345

answers:

7

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.

+3  A: 

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

Tom
Thank you so much. I see it as a philosophical minutia now. By "must", the standard means "must, or unknown behavior will ensue."
AlwaysLearning
A: 

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.

cmjreyes
+13  A: 

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

GMan
Undefined behavior is far worse than a crash.
Jurily
@Jurily: I agree, but I don't see how that's related to anything here.
GMan
Note the wording: it applies to execution, not compilation. If you enter a function with a return type other than `void`, and do not flow off the end, you don't need a `return` to avoid undefined behavior.
David Thornley
@Jurily, really? Undefined behavior could mean that it decides to format your hard drive, spam your boss with child pornography and CC the FBI on the email. Undefined behavior means it can do *anything* after that point, up to, including, and even exceeding being a complete an utter asshole. You're best failing to a known state. At least if it's crashed, I know it's *stopped* doing bad things.
Nathan Ernst
@Nathan: I think that's why Jurily said it's worse...
GMan
@GMan, my reading comprehension failed me last night...
Nathan Ernst
+6  A: 

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;
}
liori
The exception example is probably why this is just a warning. Compilers aren't very good at knowing which control paths are actually used, and an error would result in a lot of perfectly good code getting rejected.
Dennis Zickefoose
+4  A: 

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.

Charles Bailey
A: 

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.)

AndreyT
A: 

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.

daveangel