0Pricing
Mojo Academy · 课时

返回多个值

从函数返回元组

返回多个值 是 CoddyKit 上的免费 Mojo Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Mojo Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Mojo Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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.

常见问题解答

「返回多个值」课时是免费的吗?

是的 — 「返回多个值」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Mojo Academy 课程的其余内容,请升级到 CoddyKit PRO。 Mojo Academy 课程共包含 4 节课。

「返回多个值」这节课中我会学到什么?

从函数返回元组 你通过在浏览器中直接运行的动手代码来练习 Mojo Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Mojo Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Mojo Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「返回多个值」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Mojo Academy 课中编写并运行代码吗?

能。每节 Mojo Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 位置参数与关键字参数
  2. 参数的默认值
  3. 返回多个值
  4. 读取函数签名
← 返回 Mojo Academy