Returns without a "return" command
My concern is actually a problem, but not on my machine onto which the function works just fine as it is; it is a problem on my friends' laptops and IDEs, I tried copying to XCode on a friend's Macbook and it wouldn't work if the line "return min;" wasn't added at the end of the function.
Typical example of undefined behavior. Works on one machine but not another. Works at daytime but not nighttime. Works with one compiler but not another. When you invoke undefined behavior, the C standard imposes no requirements on how the code should behave.
C11 standard 6.9.1.12
If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.
In your code, that is precisely what happens. You invoke undefined behavior when you try to print the return value.
Contrary to what many believes, it IS completely allowed to omit the return statement in a non-void function. It only becomes undefined behavior if you try to use the non-existent return value.
To avoid this, always compile with at least -Wall -Wextra
.
How can a function return a variable automatically?
It follows the protocol. It knows that the return of the function should find the returned value at some location and an explicit return
from that function will fill that location. If you do not call return
, some random value will be preserved at the given location.
Is it possible that it does return the only variable that I created (static int min)?
No, it is undefined what it returns. It can be whatever.
Or is it a "problem" related to the static attribute that the variable has?
Again, what it returns is undefined.
Does it have anything to do with the nature of the function (recursive)?
Yes and no. If the function is defined as external the returned value follows another protocol as in the case of static functions.
It can happen anything in the final code, the C language does not impose the way of implementing a function, be it recursive or not. For example, in case the function is recursive and can be precomputed, only the final value can be replaced at the place of call. This is also correct as time as the final result of the program is the expected result, conforming to the operational semantics that defines C in ISO9899
.
I quote from the official document:
4 In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object).
It also can replace a call with the value of that call and this is correct.
So at all your questions, the answer is undefined behavior.