I am not saying I am the best at this, but I will give it a try.
As I understand this in the second example "foo" is returning a reference to variable "f" which goes out of scope and is destroyed when the function ends. So, back in main you are trying to use reference to a variable that no longer exists.
In main line 15 "const" does not affect the function in any way that I know of. it does say that you can not change the value of "fo" in main.
In the function "foo" making "f" on line 8 "static" allowed the program to work.
Sorry if I am not as accurate as I should be, but that is what I see.