Scrivere una funzione fn
Rigorosa, tipizzata e orientata alle prestazioni.
Scrivere una funzione fn è 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.
Meet the fn Keyword
The fn keyword defines a stricter function. It trades some flexibility for safety and the speed Mojo is famous for. ⚡
fn greet():
print("Hello")Types Are Required
Every parameter in an fn must declare its type. This lets the compiler check your code and generate fast machine code.
fn add(a: Int, b: Int) -> Int:
return a + bDeclare the Return Type
An fn states what it returns with an arrow, like -> Int. If it returns something, the type must be spelled out.
fn square(x: Int) -> Int:
return x * xNo Return Type Means None
Leave off the arrow when an fn returns nothing. It performs an action, like printing, and hands back no value.
fn shout(msg: String):
print(msg)Arguments Are Immutable by Default
In an fn, arguments are read-only by default. Trying to reassign one is a compile error, which prevents subtle bugs.
fn show(x: Int):
print(x)The Compiler Has Your Back
Because types are known, the compiler catches mismatches before the program runs, turning many runtime crashes into early errors.
Strictness Unlocks Speed
Knowing every type ahead of time lets Mojo skip dynamic checks, so an fn can reach the performance of low-level languages.
Local Variables Need var
Inside an fn, declare new local variables with var. The strict scope rules keep your function predictable.
fn total() -> Int:
var sum = 0
sum = sum + 5
return sumErrors Must Be Declared
An fn that can fail must say so with raises. Without it, the compiler assumes the function never throws.
fn risky() raises:
raise Error("nope")Same Indentation Rules
Like def, an fn uses indentation for its body. The difference is the strictness of types, not the surrounding layout.
fn info():
print("a")
print("b")Built for Hot Paths
Reach for fn in the parts of your program that run millions of times, where every saved nanosecond truly adds up.
Quick Check
Recall the default behavior of fn arguments.
Recap
You met fn: strict, typed, and fast. Types are required, arguments are read-only by default, and the compiler guards correctness. 🎯
Domande Frequenti
La lezione «Scrivere una funzione fn» è gratuita?
Sì — il testo completo di «Scrivere una funzione fn» è 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 «Scrivere una funzione fn»?
Rigorosa, tipizzata e orientata alle prestazioni. 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 «Scrivere una funzione fn»?
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
- Scrivere una funzione def
- Scrivere una funzione fn
- Quando scegliere l'una o l'altra
- Tipi restituiti e None