I read about recursion in Programming Interviews Exposed (3rd ed.) where they present the following recursive factorial
function:
int factorial(int n){
if (n > 1) { /* Recursive case */
return factorial(n-1) * n;
} else { /* Base case */
return 1;
}
}
On the bottom of the same page (page 108) they talk about tail-recursive functions:
Note that when the value returned by the recursive call is itself immediately returned, as in the preceding definition for
factorial
, the function is tail-recursive.
But is this really the case here? The last call in the function is the *
call, so won't this stack frame be preserved (if we don't take compiler optimization into account)? Is this really tail-recursive?