10 AnswersNew Answer
Undefined behaviour. I get this error when trying to compile with clang: error: non-void function 'foo' should return a value --- From the C documentation: "If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined." --- To prove this I've modified your code a little for testing with different inputs: https://code.sololearn.com/ccuyAsQ9jNF3/?ref=app
@HonFu [#GoGetThatBugChamp!] Thanks for mentioning. Er.Muthukumar The result value can be in eax register, but may not be the case on different platforms. What you are getting is garbage value. I think with current Standards, the results are undefined.
This returns 2048 . Why it happens?
Remember that val is changed in the recursive foo call. So eventually, what's returned will be an a*b*c*d... thing. Different result on Linux? Strange! Maybe we should ask someone like ~ swim ~ who *will* have the answer...
If not return statrment means, which value is actually return .... ??
HonFu [#GoGetThatBugChamp!] No,if return is (return Val )means final recursion stage Val variable contains 1 so the output will be vary but here the program output is 2048 in sololearn compiler and in Linux : 4096
but ~ swim ~ the function should at least return a value, and the value needed to be return is val
✳AsterisK✳ The function is returning a value and it is garbage value since there is no explicit value being returned.
Seems like it's val, which is declared first. You can change the return to return val, and the result is the same.
Output comes same value in Everytime. But garbage not come every time same value