The C++ standard does not define the behavior when execution flows off the end of a function that is declared to return a value (other than main
, which is special). So the behavior is a consequence of things on your particular computing platform.
First, it should be noted that any behavior not defined by the C++ standard is subject to changes during optimization by the compiler. The compiler may alter your program in radical ways, and the changes may differ drastically even with slight changes in compiler switches, source code, or other factors.
That said, processors commonly have separate registers for integer data and for floating-point data. Before your process enters the main
routine, it executes some startup code that prepares the stack and various environmental states. As that code completes, it will leave its data in registers (since it has no reason to erase it), including the register that is assigned to hold the return value of a function call. When you print the result of rand_int()
, you may be seeing some data leftover in that register.
The startup code may have no reason to use the floating-point registers, so it might not put any data in them. When you print the result of rand_float()
, you may be seeing zero because the operating system initialized the floating-point registers to zero when it created your process.