Hi all,
I am trying to understand what goes on in stack with the following code. Once its hit the base case (n == 1), how does it start unwinding?
I have the following picture attached.
http://i41.tinypic.com/21m4t90.png
1. 1 is returned to fact(n-1) where n is 2. Therefore, val = 2 * 1
2. Now that val is 2, is this returned to fact(2)?
Code:#include <stdio.h> int fact(int n) { int val; if (n == 1) return 1; else val = n * fact(n-1); //b return val; } int main() { printf("%d", fact(3)); //a getchar(); return 0; }