Beginner · Functions and arrays · topic 13 of 60 · 25 min
Recursion
Explanation
The idea
Some problems are naturally defined in terms of smaller copies of themselves: n! = n × (n − 1)!, the sum of an array is the first element plus the sum of the rest. A recursive function expresses this directly. It must have a base case that answers the smallest problem without calling itself.
unsigned long long factorial(unsigned int n) {
if (n <= 1) return 1; /* base case */
return n * factorial(n - 1); /* recursive case: a smaller problem */
}
/* factorial(5) = 120; factorial(20) = 2432902008176640000 (the largest that fits in 64 bits) */The call stack, drawn
factorial(3)
│ n = 3 → needs factorial(2)
│ │ n = 2 → needs factorial(1)
│ │ │ n = 1 → base case, returns 1
│ │ returns 2 * 1 = 2
│ returns 3 * 2 = 6
6A stack frame is the block of memory for one function call: its parameters, local variables and the return address. Frames are pushed on the call stack when a function is called and popped when it returns.
Power and binary search, recursively
double power(double base, int exp) { /* exp >= 0 */
if (exp == 0) return 1;
double half = power(base, exp / 2); /* halve the problem each time: about log2(exp) calls */
return (exp % 2 == 0) ? half * half : half * half * base;
}
int bsearch_rec(const int a[], int lo, int hi, int target) {
if (lo > hi) return -1; /* base case: empty range */
int mid = lo + (hi - lo) / 2;
if (a[mid] == target) return mid;
return (a[mid] < target) ? bsearch_rec(a, mid + 1, hi, target)
: bsearch_rec(a, lo, mid - 1, target);
}Why naive Fibonacci is slow
long long fib(int n) { return n < 2 ? n : fib(n - 1) + fib(n - 2); }
/* fib(40) makes over 300 million calls: the same values are recomputed again and again.
A simple loop computes it in 40 steps. */Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Recursion · 1/6
In this lesson you will write recursive functions, which call themselves.
Prefer a video by a teacher? Find free videos on “recursion in c factorial fibonacci call stack explained” on YouTube ↗
In real life: Folder sizes on a disk
A disk-usage tool (like du on Linux) computes the size of a folder as the size of its files plus the sizes of its sub-folders, which is naturally recursive. Real tools also limit depth or use an explicit stack to avoid stack overflow on extremely deep trees.
long long folder_size(const Folder *f) {
long long total = 0;
for (int i = 0; i < f->file_count; i++) total += f->files[i].size;
for (int i = 0; i < f->sub_count; i++) total += folder_size(&f->subs[i]); /* recursion */
return total;
}Practice (0/3 done)
What does f(4) return? int f(int n) { if (n == 0) return 0; return n + f(n - 1); }
The same recursive algorithm in Python, which runs here: write power(base, exp) for whole exp >= 0 using the halving idea (compute power(base, exp // 2) once and square it), without ** or pow.
Write recursive C functions: int sum_digits(int n) (n >= 0), int is_palindrome(const char *s, int lo, int hi) that compares the outer characters and recurses inward, and an iterative fib(n) with a loop. Test each with at least three inputs including edge cases (0, an empty or one-letter string).
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- ArraysFunctions and arrays · same module
- FunctionsFunctions and arrays · same module
- Recursion: functions that call themselvesData Structures and Algorithms (DSA) for interviews · Recursion and backtracking
- Overloading, default arguments and recursionC++ Programming (modern C++ and DSA) · Decisions, loops and functions
- Higher-order functions and recursionJavaScript Programming (language in depth) · Functions and closures
- Recursion and sorting algorithmsPython Programming · Algorithms, data structures and testing
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What must every recursive function have?
Answer: A base case
Without it, recursion never stops.
What usually happens when recursion goes too deep in C?
Answer: A stack overflow, typically a segmentation fault
The call stack runs out of space.
Why is naive recursive Fibonacci slow?
Answer: It recomputes the same values exponentially many times
fib(n-1) and fib(n-2) overlap heavily.
What is a stack frame?
Answer: The memory for one function call: parameters, locals and return address
Frames are pushed and popped on the call stack.
What happens if a recursive function has no reachable base case?Related
Answer: It keeps calling itself until Python raises RecursionError
Every call adds a frame; with no base case the stack grows until the recursion limit (about 1,000 in Python) is hit.
Why is the simple recursive Fibonacci slow for n = 45?Related
Answer: It recomputes the same smaller values a huge number of times
The number of calls grows exponentially. A loop or memoisation computes each value once.
What does every recursive function need to stop?Related
Answer: A base case
The base case answers the smallest input directly without calling itself.
What happens if a recursive function has no base case?Related
Answer: Python raises RecursionError after about 1000 nested calls
The calls keep stacking up until Python's recursion limit stops them with RecursionError.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 2 more topics in “Functions and arrays” before its exam: Arrays, 2D arrays and classic array algorithms.
- Module 3 exam: Functions and arrays · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Strings in C. 60 topics left in the course before the final exam.