Writing an fn Function
Strict, typed, and performance-focused.
Writing an fn Function is a free Mojo Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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. 🎯
Frequently asked questions
Is the “Writing an fn Function” lesson free?
Yes — the full text of “Writing an fn Function” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.
What will I learn in “Writing an fn Function”?
Strict, typed, and performance-focused. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Mojo Academy?
No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Writing an fn Function” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Mojo Academy lesson?
Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Writing a def Function
- Writing an fn Function
- When to Reach for Each
- Return Types and None