0Pricing
Zig Academy · Lesson

Recursion and Multiple Returns

Functions that call themselves and branch out.

Recursion and Multiple Returns is a free Zig Academy lesson on CoddyKit — lesson 4 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 Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

A Function Calling Itself

Recursion is when a function calls itself to solve a smaller piece of the same problem, step by step. 🔁

Every Recursion Needs a Base Case

The base case is the stopping point. Without it the function would call itself forever and overflow the stack.

The Recursive Case

The recursive case calls the function again on a smaller input, moving steadily toward the base case.

fn factorial(n: u64) u64 {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

Trace a Small Call

Calling factorial(3) becomes 3 * factorial(2), then 2 * factorial(1). The base case returns 1 and the answers multiply back up.

Recursion Uses the Stack

Each call adds a frame to the stack. Too many levels cause a stack overflow, so keep recursion depth reasonable.

Many Problems Loop Instead

Anything recursive can also be written as a loop. Loops avoid extra stack frames, so they are often the safer choice.

fn factorial(n: u64) u64 {
    var acc: u64 = 1;
    var i: u64 = 2;
    while (i <= n) : (i += 1) acc *= i;
    return acc;
}

A Function Returns One Type

A Zig function has a single return type, but it can have many return statements that all produce that one type.

Early Returns Branch Out

You can return early from different branches. Each path returns a value of the declared type, exiting the function at once.

fn sign(n: i32) i32 {
    if (n > 0) return 1;
    if (n < 0) return -1;
    return 0;
}

Return Several Values in a Struct

To hand back multiple values, return a struct or tuple that bundles them into one result.

fn divmod(a: u32, b: u32) struct { q: u32, r: u32 } {
    return .{ .q = a / b, .r = a % b };
}

Unpack the Result

The caller reads the bundled fields by name, like result.q and result.r, to use each returned piece.

const res = divmod(7, 2);
// res.q is 3, res.r is 1

Return Errors Too

A function may return an error union so a branch can signal failure while others return a normal value.

Quick Check

You write a recursive function but it never stops and crashes. What did you most likely forget?

Recap

Recursion needs a base case plus a smaller recursive call. A function has one return type but many return paths, and a struct bundles several results. 🎯

Frequently asked questions

Is the “Recursion and Multiple Returns” lesson free?

Yes — the full text of “Recursion and Multiple Returns” is free to read here on the web, and the Zig 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 Zig Academy course, upgrade to CoddyKit PRO.

What will I learn in “Recursion and Multiple Returns”?

Functions that call themselves and branch out. You practise Zig 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 Zig Academy?

No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Recursion and Multiple Returns” 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 Zig Academy lesson?

Yes. Every Zig 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. Function Syntax and Return Types
  2. Passing Values vs References
  3. pub Functions and Visibility
  4. Recursion and Multiple Returns
← Back to Zig Academy