0Pricing
Mojo Academy · Lezione

Argomenti posizionali e keyword

Passi i valori per posizione o per nome.

Argomenti posizionali e keyword è una lezione Mojo Academy gratuita su CoddyKit. Questa è la lezione 1 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.

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 match

Keyword 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 result

Mixing 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)  # error

Why 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.

Domande Frequenti

La lezione «Argomenti posizionali e keyword» è gratuita?

Sì — il testo completo di «Argomenti posizionali e keyword» è 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 «Argomenti posizionali e keyword»?

Passi i valori per posizione o per nome. 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 1 di 4.

Quanto tempo richiede la lezione «Argomenti posizionali e keyword»?

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

  1. Argomenti posizionali e keyword
  2. Valori predefiniti degli argomenti
  3. Restituire più valori
  4. Leggere le firme delle funzioni
← Torna a Mojo Academy