I am getting closer to debunking this recursive mystery, there is only one thing left that I can not trace in this line of the code, and that is the final return value wich is 243 if i call rec() passing it the value 5. this should be the trace:
n: 4 *3: 12
n: 3 *3: 9
n: 2 *3: 6
n: 1 *3: 3
n: 0 *3: 0
n: 1 *3: 3
result: 243
Correct? how does it get the result of 243?
int rec(int n)
{
if (n == 0)
return 1;
return 3 * rec(n-1);
}
Your function computes : 3^n.
The number 3 is multiplied with the result of the n-1 calls.
f(n) = 3 * f(n-1);
f(0) = 1;
f(1) = 3 * f(0) = 3 * 1 = 3;
f(2) = 3 * f(1) = 3 * 3 = 9;
f(3) = 3 * f(2) = 3 * 3 * f(1) = 3 * 3 * 3 = 27
.
.
.
f(5) = 3 * 3 * 3 *3 * 3 = 243