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

前端 未结 7 1407
忘了有多久
忘了有多久 2020-11-27 07:31

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 wit

相关标签:
7条回答
  • 2020-11-27 07:56

    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.

    0 讨论(0)
  • 2020-11-27 07:59

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

    0 讨论(0)
  • 2020-11-27 08:00

    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.

    0 讨论(0)
  • 2020-11-27 08:10

    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

    0 讨论(0)
  • 2020-11-27 08:11

    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.

    0 讨论(0)
  • 2020-11-27 08:11

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

    0 讨论(0)
提交回复
热议问题