0

Why does a C++ function that returns a numeric value such as int or double work just fine if I don't include the return statement? Should I avoid that even though the program runs fine? Thanks

  • 1
    How did you check that it works fine? And yes, you should avoid such as thing and you will get some warnings from the compiler. – MikeCAT Nov 26 '15 at 14:22
  • I guess because it compiles and runs. – Abdulaziz Asiri Nov 26 '15 at 14:24
  • 1
    Your questions have already been answered : [link](http://stackoverflow.com/questions/1610030/why-can-you-return-from-a-non-void-function-without-returning-a-value-without-pr) – SwissFr Nov 26 '15 at 14:24

1 Answers1

3

It could "work fine" for two reasons:

  1. Your function is not main(), in which case flowing off the end with no return statement causes undefined behaviour1, and it just happens that it seems to work fine. But it doesn't really.
  2. The function is main(), which has an implicit return 0;.

For case 1, you should consider your code badly broken and fix it.


1 §6.3.3 [stmt.return] in the C++11 standard

juanchopanza
  • 223,364
  • 34
  • 402
  • 480