CHAPTER 18
Beginner
Recursion in C
Updated: May 17, 2026
5 min read
# CHAPTER 18
Recursion in C
1. Introduction
To understand recursion, you must first understand recursion. Jokes aside, Recursion occurs when a function calls itself. While loops repeat a block of code, recursion repeats a function call. It is an incredibly powerful tool for breaking down complex problems (like traversing trees or sorting arrays) into simpler sub-problems.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand how recursive functions work.
- Identify the Base Case and the Recursive Case.
- Trace recursive calls through the Call Stack.
- Write a recursive function to find the factorial of a number.
- Understand the dangers of Stack Overflow.
3. The Anatomy of Recursion
Every recursive function MUST have two parts:- 1. The Base Case: The condition where the function stops calling itself and returns a value. Without this, the function runs infinitely!
- 2. The Recursive Case: The part where the function calls itself with a slightly modified (smaller) input.
4. Example: Factorial
The factorial of 5 (written as5!) is 5 * 4 * 3 * 2 * 1.
Notice the pattern: 5! = 5 * 4!. Therefore, n! = n * (n-1)!.
c
5. The Call Stack (How Recursion Works in Memory)
Let's tracefactorial(3):
-
1.
main()callsfactorial(3).
-
2.
factorial(3)runs.n=3. Not base case. It returns3 * factorial(2). (It pauses here and waits).
-
3.
factorial(2)runs.n=2. Not base case. It returns2 * factorial(1). (It pauses).
-
4.
factorial(1)runs.n=1. Base case! Returns1.
Now the stack unwinds!
-
factorial(2)finishes:2 * 1 = 2. Returns2.
-
factorial(3)finishes:3 * 2 = 6. Returns6back to main!
6. Example: Fibonacci Sequence
The sequence:0, 1, 1, 2, 3, 5, 8, 13...
Formula: fib(n) = fib(n-1) + fib(n-2)
c
7. Common Mistakes (Stack Overflow)
If you forget the Base Case, or if the recursive step doesn't progress toward the base case, the function will call itself forever. Because every function call creates a new Stack Frame in RAM, the computer will eventually run out of Stack memory. This crashes the program with a Stack Overflow.
c
8. Iteration (Loops) vs. Recursion
Anything done with a loop can be done with recursion, and vice versa.- Loops: Faster, use less memory (O(1) space).
- Recursion: Slower, uses more memory (due to stack frames), but makes complex algorithms (like QuickSort or Tree Traversal) much easier to read and write.
9. Exercises
-
1.
Write a recursive function to calculate the sum of numbers from 1 to
N.
-
2.
Write a recursive function that prints numbers from
Ndown to 1.
-
3.
Trace the call stack on paper for
fibonacci(3). How many times is the function called?
10. MCQ Quiz with Answers
Question 1
What is a recursive function?
Question 2
What is the essential condition to stop recursion?
Question 3
What happens if a recursive function has no base case?
Question 4
Where are recursive function calls stored in memory?
Question 5
Recursion is usually ________ than loops.
Question 6
What does the following return for func(3)? int func(int n) { if(n==1) return 1; return n + func(n-1); }
Question 7
The Fibonacci sequence is commonly taught using recursion. What is fib(0)?
Question 8
When a recursive call reaches the base case, the stack begins to:
Question 10
Why use recursion if loops are faster?
11. Interview Questions
- Q: What is Tail Recursion, and how does the compiler optimize it?
- Q: Explain why finding Fibonacci numbers using naive recursion is extremely inefficient (O(2^n)).
- Q: How do you convert a recursive function into an iterative one? (Hint: use a Stack data structure).
12. Summary
Recursion is when a function calls itself. It requires a Base Case to stop, and a Recursive Case to continue. While elegant and useful for complex data structures, it uses Stack memory heavily and can lead to a Stack Overflow if written incorrectly.13. Next Chapter Recommendation
In Chapter 19: Storage Classes, we will look at how C manages the lifespan and visibility of variables usingauto, static, extern, and register.