複数の値を返す
関数からタプルを返します
「複数の値を返す」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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) # 3Names 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]) # 3Why 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 successTuples 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.
よくある質問
「複数の値を返す」レッスンは無料ですか?
はい。「複数の値を返す」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。
「複数の値を返す」で何を学びますか?
関数からタプルを返します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Mojo Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「複数の値を返す」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMojo Academyレッスンでコードを書いて実行できますか?
はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。