0Pricing
Mojo Academy · Lesson

Returning Multiple Values

Hand back tuples from a function.

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

More Than One Result

Sometimes a function naturally produces two answers at once. In Mojo you can hand them all back together using a tuple.

Returning a Tuple

List the values separated by commas in the return. Mojo packs them into one tuple that the caller can take apart.

fn min_max(a: Int, b: Int) -> (Int, Int):
    if a < b:
        return (a, b)
    return (b, a)

Declaring the Tuple Type

The arrow type spells out each slot. Here (Int, Int) tells readers and the compiler exactly what comes back.

fn split() -> (String, Int): ...

Unpacking the Result

The caller can unpack the tuple into separate names in one line, giving each value a clear identity right away.

lo, hi = min_max(8, 3)
print(lo)  # 3

Names Match Order

Unpacking follows position. The first name takes the first value, the second name the second, so the order must line up with the return.

low, high = min_max(8, 3)

Or Keep the Whole Tuple

You do not have to unpack. Hold the tuple in one variable and reach into it by index when you need a piece.

pair = min_max(8, 3)
print(pair[0])  # 3

Why Not Just One Return?

Bundling related results keeps them together. Returning both a value and its status in one tuple is cleaner than scattering them across globals.

fn parse() -> (Int, Bool): ...  # value and success

Tuples Can Mix Types

A tuple is not limited to one type. You can return a String and an Int side by side, each keeping its own type.

fn user() -> (String, Int):
    return ("Ada", 36)

Reading Multi-Value Returns

When you see an arrow leading to parentheses, expect several results. The signature is your map for how to unpack them.

Keep Tuples Small and Clear

Two or three values in a tuple read well. If you need many, a named struct usually communicates intent better than a long tuple.

A Common Pattern

Returning a result plus an error or flag is a classic use of tuples. The caller unpacks both and reacts to each in turn.

value, ok = try_parse(text)

Quick Check

How does a Mojo function hand back more than one value at once?

Recap: Bundle and Unpack

Return several results as a tuple, declare its type in the signature, then unpack by position. Keep tuples small or reach for a struct.

Frequently asked questions

Is the “Returning Multiple Values” lesson free?

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

What will I learn in “Returning Multiple Values”?

Hand back tuples from a function. You practise Mojo 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 Mojo Academy?

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

How long does the “Returning Multiple Values” 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 Mojo Academy lesson?

Yes. Every Mojo 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. Positional and Keyword Arguments
  2. Default Argument Values
  3. Returning Multiple Values
  4. Reading Function Signatures
← Back to Mojo Academy