Recursion in C
Explore recursive functions, understand their use cases, and analyze recursion vs. iteration.
Recursion in C is a free C Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Recursion in C
Recursion is a technique where a function calls itself to solve a problem.
In this lesson, you will learn:
- What recursion is and how it works.
- How to implement recursive functions.
- The difference between recursion and iteration.

2
What is Recursion?
Recursion is when a function calls itself to solve a smaller part of a problem.
Example syntax:
void function() {
function(); // Recursive call
}
Every recursive function must have a base case to prevent infinite recursion.
3
Example: Recursion
This program demonstrates recursion by printing numbers from 5 to 1.
#include <stdio.h>
void countDown(int n) {
if (n <= 0) return; // Base case
printf("%d\n", n);
countDown(n - 1); // Recursive call
}
int main() {
countDown(5);
return 0;
}4
Base Case in Recursion
A recursive function must include a base case to stop recursion.
Example:
if (n == 0) return;
Without a base case, the function will call itself infinitely, causing a stack overflow.
5
Example: Factorial Using Recursion
This program calculates the factorial of a number using recursion.
#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1; // Base case
return n * factorial(n - 1);
}
int main() {
printf("Factorial of 5: %d\n", factorial(5));
return 0;
}6
Recursion vs Iteration
Recursion and iteration (loops) can achieve the same results, but they work differently.
Differences:
- Recursion uses function calls and requires more memory.
- Iteration uses loops and is generally more efficient.
- Recursion makes code simpler for problems like tree traversal.
7
Example: Fibonacci Sequence (Recursive)
This program calculates the Fibonacci sequence using recursion.
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
printf("Fibonacci(5): %d\n", fibonacci(5));
return 0;
}9
When to Use Recursion?
Recursion is useful for problems that involve:
- Tree traversal.
- Backtracking (e.g., solving mazes).
- Mathematical problems like factorial and Fibonacci.
10
Summary
In this lesson, you learned:
- What recursion is and how it works.
- The importance of a base case.
- The difference between recursion and iteration.
Next, we will explore arrays and strings in C!

Frequently asked questions
Is the “Recursion in C” lesson free?
Yes — the full text of “Recursion in C” is free to read here on the web, and the C Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.
What will I learn in “Recursion in C”?
Explore recursive functions, understand their use cases, and analyze recursion vs. iteration. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Recursion in C” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.