0
0
DSA Cprogramming~10 mins

Recursion Base Case and Recursive Case in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Recursion Base Case and Recursive Case
Start Recursive Function
Check Base Case?
YesReturn Base Value
No
Perform Recursive Call with Smaller Input
Combine Result and Return
End
The function checks if it reached the simplest case (base case). If yes, it returns a value. Otherwise, it calls itself with a smaller input and combines the result.
Execution Sample
DSA C
int factorial(int n) {
  if (n == 0) return 1;
  return n * factorial(n - 1);
}
Calculates factorial of n using recursion with a base case when n is 0.
Execution Table
StepCall StackCurrent nCondition (n==0?)ActionReturn ValueVisual Call Stack
1factorial(3)3NoCall factorial(2)factorial(3)
2factorial(3) -> factorial(2)2NoCall factorial(1)factorial(3) └─ factorial(2)
3factorial(3) -> factorial(2) -> factorial(1)1NoCall factorial(0)factorial(3) └─ factorial(2) └─ factorial(1)
4factorial(3) -> factorial(2) -> factorial(1) -> factorial(0)0YesReturn 11factorial(3) └─ factorial(2) └─ factorial(1) └─ factorial(0)
5factorial(3) -> factorial(2) -> factorial(1)1-Return 1 * 1 = 11factorial(3) └─ factorial(2) └─ factorial(1)
6factorial(3) -> factorial(2)2-Return 2 * 1 = 22factorial(3) └─ factorial(2)
7factorial(3)3-Return 3 * 2 = 66factorial(3)
8---All calls returned, final result = 66-
💡 Recursion ends when base case n==0 is reached and all calls return their results.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
n332101233
Return Value11266
Call Stack Depth123443210
Key Moments - 3 Insights
Why does the recursion stop at n == 0 and not before?
Because the base case condition (n == 0) is checked at each call (see Step 4 in execution_table). When true, it returns a value without further calls, stopping recursion.
What happens to the call stack when a recursive call returns?
The call stack shrinks as each function returns its result (see Steps 5 to 7). Each return removes the top call, moving back to the previous call.
Why do we multiply n by factorial(n-1) after the recursive call?
Because factorial(n) = n * factorial(n-1). The multiplication happens after the recursive call returns its value (see Step 5 and onwards).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4, what is the return value when n == 0?
An
B0
C1
DUndefined
💡 Hint
Check the 'Return Value' column at Step 4 in execution_table.
At which step does the call stack start to shrink?
AStep 3
BStep 5
CStep 4
DStep 7
💡 Hint
Look at the 'Call Stack' and 'Visual Call Stack' columns from Step 4 to Step 5.
If the base case was changed to n == 1, how would the execution_table change?
ARecursion would stop one step earlier, base case reached at n==1
BRecursion would stop earlier, base case reached at n==0
CNo change in recursion steps
DRecursion would never stop
💡 Hint
Think about when the base case condition is true and when recursion stops.
Concept Snapshot
Recursion has two parts:
- Base Case: stops recursion (e.g., if n==0 return 1)
- Recursive Case: calls itself with smaller input (e.g., n * factorial(n-1))
Each call waits for the next call to return before finishing.
Without base case, recursion never ends and causes error.
Full Transcript
Recursion works by a function calling itself with smaller inputs until it reaches a base case. The base case stops further calls and returns a simple value. Then each previous call uses that returned value to compute its own result and return it. For example, factorial(3) calls factorial(2), which calls factorial(1), which calls factorial(0). At factorial(0), the base case returns 1. Then factorial(1) returns 1*1=1, factorial(2) returns 2*1=2, and factorial(3) returns 3*2=6. The call stack grows with each call and shrinks as calls return. This process ensures the problem is solved step-by-step.