Dynamic Programming21 sections · 916 units
Open in Course

Factorial - Walkthrough

Tracing factorial(4)

Let me trace factorial(44) step by step. Each call waits for the next to return. factorial(44) calls factorial(33), which calls factorial(22), which calls factorial(11), which calls factorial(00). factorial(00) hits the base case and returns 11.

Now the chain unwinds: factorial(11) returns 1×1=11 \times 1 = 1, factorial(22) returns 2×1=22 \times 1 = 2, factorial(33) returns 3×2=63 \times 2 = 6, factorial(44) returns 4×6=244 \times 6 = 24. Notice how results multiply back up. The recursion "remembers" each waiting call. This is the call stack in action.

unnamed (5) (1).jpg