Why is this piece of code compiling?
#include <iostream> int foo(int x) { if(x == 10) return x*10; } int main() { int a; std::cin>>a; std::cout<<foo(a)<<'\n'; }
The compiler should not give me an error, for example, "not all code paths return a value"? What happens / returns my function when x is not equal to ten?
The result is undefined, so the compiler can choose - you probably get what happens to sit at the appropriate stack address where the caller expects the result. Activate compiler warnings and your compiler will inform you of your omission.
The compiler is not required to inform you of this. Many will, some will give warnings. Some, apparently, will not notice.
, , . ( , ).
, , , undefined. . . - . undefined.