Restituire più valori
Restituisca tuple da una funzione.
Restituire più valori è una lezione Mojo Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Mojo Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Mojo Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Domande Frequenti
La lezione «Restituire più valori» è gratuita?
Sì — il testo completo di «Restituire più valori» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Mojo Academy, passa a CoddyKit PRO. Il corso Mojo Academy include 4 lezioni in totale.
Cosa imparerò in «Restituire più valori»?
Restituisca tuple da una funzione. Eserciti Mojo Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Mojo Academy?
Non è richiesta alcuna esperienza precedente. Mojo Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.
Quanto tempo richiede la lezione «Restituire più valori»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Mojo Academy?
Sì. Ogni lezione Mojo Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Argomenti posizionali e keyword
- Valori predefiniti degli argomenti
- Restituire più valori
- Leggere le firme delle funzioni