Try compiling with the -Wall(gcc) [ -Wreturn-type] option . You will receive a warning that “Control reaches the end of the non-void function” or something like “no return statement in the function returning the non-void”
Example:
C:\Users\SUPER USER\Desktop>type no_return.cpp
int func(){}
int main()
{
int z = func();
std::cout<< z; //Undefined Behaviour
}
C:\Users\SUPER USER\Desktop>g++ -Wall no_return.cpp
no_return.cpp: In function 'int func()':
no_return.cpp:2:12: warning: no return statement in function returning non-void
C:\Users\SUPER USER\Desktop>
Using the return value of a non-boolean function (without a return statement) Undefined Behavior.
source
share