Parametri e argomenti
Valori a tempo di compilazione e valori a runtime.
Parametri e argomenti è 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 Kinds of Input
Mojo functions can take two kinds of input: parameters known at compile time, and arguments known at runtime. Knowing the difference is the key.
Arguments Live at Runtime
An argument is an ordinary value you pass when the program runs. Its actual number can change every single time you call the function.
fn square(x: Int) -> Int:
return x * xParameters Live at Compile Time
A parameter is a value the compiler already knows before your program runs. It is fixed and baked right into the generated code. ⚙️
Brackets vs Parentheses
Mojo keeps them visually separate: parameters go in square brackets, while arguments stay in the familiar parentheses.
fn repeat[count: Int](msg: String):
print(msg)Reading the Signature
In repeat[count: Int](msg: String), the count is set at compile time and msg is supplied later when you actually call it.
Compile Time Means Earlier
Think of compile time as the moment Mojo translates your code, long before it runs. Parameters are decided in that earlier phase.
Why Separate Them
Keeping compile-time and runtime values separate lets the compiler specialize and optimize code that the runtime simply cannot.
Passing a Parameter
You supply a parameter in brackets at the call site, just like you supply arguments in parentheses right after it.
repeat[3]("hi")Parameters Must Be Known
A parameter value must be a compile-time constant. You cannot pass a number you only computed while the program was running.
A Familiar Example
The built-in SIMD type takes its element type and width as parameters, then holds real numbers as runtime values inside it.
var v = SIMD[DType.int32, 4](1, 2, 3, 4)The Mental Model
Picture two slots: the bracket slot for compile-time facts, the parenthesis slot for runtime data. Mojo treats them very differently.
Quick Check
Tell apart parameters and arguments in Mojo.
Recap
You learned the split: parameters are compile-time values in brackets, arguments are runtime values in parentheses. That divide powers Mojo's speed. 🎯
Domande Frequenti
La lezione «Parametri e argomenti» è gratuita?
Sì — il testo completo di «Parametri e 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 «Parametri e argomenti»?
Valori a tempo di compilazione e valori a runtime. 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 «Parametri e 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
- Parametri e argomenti
- Funzioni parametrizzate
- Struct parametrizzate
- Perché il tempo di compilazione aumenta la velocità