Valori predefiniti degli argomenti
Renda facoltativi alcuni parametri.
Valori predefiniti degli argomenti è una lezione Mojo Academy gratuita su CoddyKit. Questa è la lezione 2 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.
Making a Parameter Optional
A default value lets a parameter stand in for itself. If the caller skips it, Mojo quietly uses the value you wrote in the definition.
fn greet(name: String, times: Int = 1):
for _ in range(times):
print(name)Calling With and Without
Now both calls work. Omit times and you get the default of 1; pass it and you override the default for that one call.
greet("Mojo") # times = 1
greet("Mojo", 3) # times = 3Why Defaults Are Handy
Defaults capture the common case. Most callers never touch the option, yet the power is there for the few who need it.
Defaults Go Last
Parameters with defaults must come after required ones. Otherwise Mojo could not tell which value you meant to skip.
fn open(path: String, mode: String = "r"): ... # okA Required-Then-Optional Shape
Think of it as required-first, optional-last. Reading left to right, you supply what is needed, then accept defaults for the rest. This shape is predictable.
Overriding Just One Default
To change a later default while keeping earlier ones, use a keyword. You skip straight to the option you care about.
fn make(width: Int = 80, height: Int = 24): ...
make(height=40) # width stays 80Multiple Defaults Together
A function can give several parameters defaults at once. Callers fill only what they want and let the rest fall back.
fn box(w: Int = 10, h: Int = 10, fill: String = "x"): ...Defaults Build Friendly APIs
Good defaults make a function easy to start with and powerful to grow into. Beginners call it plainly; experts tune the options.
Choosing a Sensible Default
Pick the value most callers would want anyway. A surprising default is worse than none, because it hides behavior the reader did not expect.
The Default Is Evaluated Once
The default expression belongs to the signature, not the body. Treat it as a fixed fallback that documents the normal value.
fn retry(count: Int = 3): ... # 3 is the documented normDefaults Plus Keywords Shine
Defaults and keyword calls are a great pair. Together they let you write functions with many knobs that still feel simple to call.
request(url, timeout=30, retries=5)Quick Check
Where must a parameter with a default value be placed in a Mojo function?
Recap: Smart Fallbacks
Default values make parameters optional and capture the common case. Put them last, choose them wisely, and override single ones with keywords.
Domande Frequenti
La lezione «Valori predefiniti degli argomenti» è gratuita?
Sì — il testo completo di «Valori predefiniti degli argomenti» è 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 «Valori predefiniti degli argomenti»?
Renda facoltativi alcuni parametri. 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 2 di 4.
Quanto tempo richiede la lezione «Valori predefiniti degli argomenti»?
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