I encountered this problem in my program which was creating problems. so let me explain it by a example:
#include<iostream>
int func(){
if(1==0) return 100;
}
int main(){
int x=99;
x= func();
std::cout<<"Value of x: " << x <<std::endl;
}
I had thought the output will be 99 but output is 0, so what’s going on here?
You have undefined behavior because your function is declared to return an
intbut the execution path through the function never reaches areturnstatement.In C++ it is illegal to exit a function defined as returning a non-
voidtype other than via a return statement with an argument.ISO/IEC 14882:2003 6.6.3 [stmt.return] / 2: