Zwracanie wielu wartości
Zwracaj krotki z funkcji.
Zwracanie wielu wartości to bezpłatna lekcja Mojo Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Mojo Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Mojo Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Często zadawane pytania
Czy lekcja „Zwracanie wielu wartości” jest bezpłatna?
Tak — pełny tekst „Zwracanie wielu wartości” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Mojo Academy, przejdź na CoddyKit PRO. Kurs Mojo Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Zwracanie wielu wartości”?
Zwracaj krotki z funkcji. Ćwiczysz Mojo Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Mojo Academy?
Nie wymagamy żadnego doświadczenia. Mojo Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.
Ile czasu zajmuje lekcja „Zwracanie wielu wartości”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Mojo Academy?
Tak. Każda lekcja Mojo Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Argumenty pozycyjne i nazwane
- Domyślne wartości argumentów
- Zwracanie wielu wartości
- Odczytywanie sygnatur funkcji