0Pricing
C Academy · Lesson

How Recursion Works

Base cases and the call stack.

How Recursion Works is a free C Academy lesson on CoddyKit — lesson 1 of 4. 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 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is Recursion?

Recursion is when a function calls itself to solve a problem. Each call works on a smaller piece of the original problem.

In C, any function can call itself, as long as there is a way for the calls to eventually stop.

The Base Case

Every recursive function needs a base case: a condition where it stops calling itself and returns directly.

Without a base case, the function would call itself forever and crash the program.

int countdown(int n) {
    if (n == 0) return 0; /* base case */
    return countdown(n - 1);
}

The Recursive Case

The recursive case is the part where the function calls itself with a changed argument.

That argument must move toward the base case, otherwise recursion never ends.

int sum_to(int n) {
    if (n == 0) return 0;       /* base case */
    return n + sum_to(n - 1);   /* recursive case */
}

A First Full Program

Let us run a complete program that sums numbers from 1 to 5 using recursion.

The result should be 15.

#include <stdio.h>

int sum_to(int n) {
    if (n == 0) return 0;
    return n + sum_to(n - 1);
}

int main(void) {
    printf("%d\n", sum_to(5));
    return 0;
}

Tracing the Calls

It helps to trace recursion by hand. For sum_to(3):

sum_to(3) = 3 + sum_to(2)
sum_to(2) = 2 + sum_to(1)
sum_to(1) = 1 + sum_to(0)
sum_to(0) = 0

The calls then return back up: 1, then 3, then 6.

The Call Stack

Each function call gets its own space on the call stack, holding its parameters and local variables.

While going deeper, frames pile up. When a call returns, its frame is removed and control goes back to the caller.

Winding and Unwinding

Recursion has two phases. Winding is when calls keep going deeper toward the base case.

Unwinding is when the base case returns and each call finishes its work using the returned value.

#include <stdio.h>

int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

int main(void) {
    printf("%d\n", factorial(4));
    return 0;
}

Return Values Flow Back

The value returned by a deeper call is used by the call that made it.

This is why order matters: the deepest call finishes first, then results combine on the way back up the stack.

int power(int base, int exp) {
    if (exp == 0) return 1;
    return base * power(base, exp - 1);
}

Printing During Recursion

You can print before or after the recursive call. Printing before shows numbers going down; printing after shows them coming back up.

#include <stdio.h>

void down(int n) {
    if (n == 0) return;
    printf("%d ", n);
    down(n - 1);
}

int main(void) {
    down(5);
    printf("\n");
    return 0;
}

Print On The Way Up

Move the printf after the recursive call and the order reverses. The deepest call prints first.

This prints 1 2 3 4 5 instead of 5 4 3 2 1.

#include <stdio.h>

void up(int n) {
    if (n == 0) return;
    up(n - 1);
    printf("%d ", n);
}

int main(void) {
    up(5);
    printf("\n");
    return 0;
}

Two Rules To Remember

A correct recursive function follows two rules:

1. It has at least one base case that returns without recursing.
2. Every recursive call moves the argument closer to a base case.

Break either rule and the program loops forever.

Quick Check

Test your understanding of recursion basics.

Recap

Recursion solves a problem by calling itself on a smaller input. You always need a base case to stop and a recursive case that moves toward it.

Each call uses a stack frame; results flow back as calls unwind.

Frequently asked questions

Is the “How Recursion Works” lesson free?

Yes — the full text of “How Recursion Works” is free to read here on the web, and the C Academy course includes 4 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 “How Recursion Works”?

Base cases and the call stack. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “How Recursion Works” 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. How Recursion Works
  2. Classic Recursive Problems
  3. Recursion vs Iteration
  4. Avoiding Stack Overflow
← Back to C Academy