C Academy · Lesson

Recursion in C

Explore recursive functions, understand their use cases, and analyze recursion vs. iteration.

Lesson 3 of 39 steps

Recursion in C is a free C Academy lesson on CoddyKit. This is 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, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 3 lessons in total.

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.
Recursion in C — illustration 1

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.

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;
}

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.

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;
}

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.

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;
}

When to Use Recursion?

Recursion is useful for problems that involve:

  • Tree traversal.
  • Backtracking (e.g., solving mazes).
  • Mathematical problems like factorial and Fibonacci.

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!

Recursion in C — illustration 9
Free to start

Learn C with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
39
Lessons
144

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. 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. The C Academy course includes 3 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 3.

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.

All lessons in this course

  1. Defining and Calling Functions
  2. Function Prototypes and Scope
  3. Recursion in C
← Back to C Academy