0
0
DSA Cprogramming~10 mins

Fibonacci Using Recursion in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Fibonacci Using Recursion
Call fibonacci(n)
Is n <= 1?
YesReturn n
No
Call fibonacci(n-1)
Call fibonacci(n-2)
Add results of fibonacci(n-1) + fibonacci(n-2)
Return sum
End call
The function calls itself with smaller values until it reaches the base case, then returns and sums results back up.
Execution Sample
DSA C
int fibonacci(int n) {
    if (n <= 1) return n;
    return fibonacci(n-1) + fibonacci(n-2);
}
This code calculates the nth Fibonacci number by calling itself with n-1 and n-2 until it reaches 0 or 1.
Execution Table
StepCall Stack (Top)Current nActionReturn ValueStack Depth
1fibonacci(4)4Check if n <= 1 (No), call fibonacci(3)1
2fibonacci(3)3Check if n <= 1 (No), call fibonacci(2)2
3fibonacci(2)2Check if n <= 1 (No), call fibonacci(1)3
4fibonacci(1)1n <= 1 (Yes), return 114
5fibonacci(0)0n <= 1 (Yes), return 004
6fibonacci(2)2Return fibonacci(1)+fibonacci(0) = 1+013
7fibonacci(1)1n <= 1 (Yes), return 113
8fibonacci(3)3Return fibonacci(2)+fibonacci(1) = 1+122
9fibonacci(2)2Check if n <= 1 (No), call fibonacci(1)2
10fibonacci(1)1n <= 1 (Yes), return 113
11fibonacci(0)0n <= 1 (Yes), return 003
12fibonacci(2)2Return fibonacci(1)+fibonacci(0) = 1+012
13fibonacci(4)4Return fibonacci(3)+fibonacci(2) = 2+131
14--All calls complete, final result = 330
💡 Recursion ends when n <= 1, returning n; all calls then return sums up to the original call.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 6After Step 8After Step 13Final
n44322344
Return Value11233
Stack Depth01233210
Key Moments - 3 Insights
Why does fibonacci(2) get called multiple times?
Because each call to fibonacci(n) calls fibonacci(n-1) and fibonacci(n-2) separately, causing repeated calls for the same n, as seen in steps 3, 6, 9, and 12.
Why do we return n immediately when n <= 1?
Because n=0 or n=1 are the base cases of the Fibonacci sequence, stopping recursion and providing known values, as shown in steps 4, 5, 7, 10, and 11.
How does the call stack depth change during recursion?
The stack depth increases as recursive calls go deeper (steps 1 to 4), then decreases as calls return (steps 6, 8, 13), shown clearly in the Stack Depth column.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 4, what is the return value of fibonacci(1)?
A1
B0
C2
DUndefined
💡 Hint
Check the Return Value column at step 4 in the execution_table.
At which step does the recursion start to return values back up the call stack?
AStep 1
BStep 6
CStep 4
DStep 14
💡 Hint
Look for the first step where Return Value is filled in the execution_table.
If we call fibonacci(5) instead of fibonacci(4), how would the final return value change?
AIt would be 4
BIt would be 5
CIt would be 3
DIt would be 8
💡 Hint
Recall Fibonacci sequence: fibonacci(4) returns 3, fibonacci(5) returns 5.
Concept Snapshot
Fibonacci Using Recursion:
- Base case: if n <= 1, return n
- Recursive case: return fibonacci(n-1) + fibonacci(n-2)
- Calls build up until base case, then sum back up
- Call stack depth grows with n
- Repeated calls cause inefficiency without memoization
Full Transcript
This visualization traces the recursive Fibonacci function for n=4. The function calls itself with smaller values until it reaches the base case where n is 0 or 1, returning n immediately. Each call waits for the results of fibonacci(n-1) and fibonacci(n-2), then sums them and returns the result. The call stack grows as calls nest deeper and shrinks as they return. The execution table shows each step, the current call, action, return values, and stack depth. Key moments include understanding base cases, repeated calls, and stack depth changes. The quiz tests understanding of return values, recursion return timing, and Fibonacci sequence results. This method is simple but inefficient due to repeated calls.