Positions- und Keyword-Argumente
Übergeben Sie Werte nach Position oder Namen.
Positions- und Keyword-Argumente ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 1 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.
Two Ways to Pass Values
Every Mojo function call sends in values called arguments. You can send them by position or by name, and both styles can live in one call.
Positional Arguments
With positional arguments, order is everything. The first value fills the first parameter, the second fills the second, and so on.
fn greet(name: String, times: Int):
for _ in range(times):
print(name)
greet("Mojo", 2)Order Matters Here
Swap two positional arguments and the meaning changes. Here Mojo would read 2 as the name and "Mojo" as the count, so order must match the parameters.
greet(2, "Mojo") # wrong: positions no longer matchKeyword Arguments
A keyword argument names the parameter you mean. Now the call reads clearly and order stops mattering.
greet(name="Mojo", times=2)Keywords Free You from Order
Because each value is labeled, you can list keyword arguments in any order. Mojo matches them by name, not by position.
greet(times=2, name="Mojo") # same resultMixing Both Styles
You can combine the two, but positional values must come first. Once a keyword appears, everything after it must also be a keyword.
greet("Mojo", times=2) # ok
# greet(name="Mojo", 2) # errorWhy Keywords Help Readers
Keyword arguments turn a mystery call into a sentence. Reading times=2 tells you exactly what the 2 does, even months later.
Avoiding Position Bugs
When a function takes several same-typed values, positions are easy to mix up. Naming them with keywords removes that whole class of bug.
draw(width=800, height=600) # clearer than draw(800, 600)Names Come from Parameters
The keyword you type must match the parameter name in the function definition. A typo like nme= is an error, not a new argument.
fn greet(name: String, times: Int): ...
greet(name="Mojo", times=2)Both Styles, One Function
You do not pick a style per function. The caller decides, so the same function accepts positional, keyword, or a mix on each call.
A Practical Rule of Thumb
Use positional for the obvious leading value, then switch to keywords for extras. This keeps short calls tidy and long calls clear.
connect("localhost", port=8080, retries=3)Quick Check
You want order to stop mattering in a function call. Which approach helps?
Recap: By Position or Name
Positional arguments rely on order; keyword arguments rely on names. Lead with positions, label the rest, and your calls stay both short and clear.
Häufig gestellte Fragen
Ist die Lektion „Positions- und Keyword-Argumente“ kostenlos?
Ja — der vollständige Text von „Positions- und Keyword-Argumente“ 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 „Positions- und Keyword-Argumente“?
Übergeben Sie Werte nach Position oder Namen. 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 1 von 4.
Wie lange dauert die Lektion „Positions- und Keyword-Argumente“?
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
- Positions- und Keyword-Argumente
- Standardwerte für Argumente
- Mehrere Werte zurückgeben
- Funktionssignaturen lesen