0Pricing
Mojo Academy · Lektion

Mehrere Werte zurückgeben

Geben Sie Tupel aus einer Funktion zurück.

Mehrere Werte zurückgeben ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Mojo Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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.

Häufig gestellte Fragen

Ist die Lektion „Mehrere Werte zurückgeben“ kostenlos?

Ja — der vollständige Text von „Mehrere Werte zurückgeben“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Mojo Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Mehrere Werte zurückgeben“?

Geben Sie Tupel aus einer Funktion zurück. Du übst Mojo Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Mojo Academy zu starten?

Keine Vorkenntnisse erforderlich. Mojo Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.

Wie lange dauert die Lektion „Mehrere Werte zurückgeben“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Mojo Academy-Lektion Code schreiben und ausführen?

Ja. Jede Mojo Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Positions- und Keyword-Argumente
  2. Standardwerte für Argumente
  3. Mehrere Werte zurückgeben
  4. Funktionssignaturen lesen
← Zurück zu Mojo Academy