When to Reach for Each
Match the function style to your goal.
When to Reach for Each is a free Mojo Academy lesson on CoddyKit — lesson 3 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.
Two Tools, One Choice
Mojo gives you both def and fn. Picking well means matching the tool to what your code actually needs. 🧭
Choose def for Flexibility
Pick def when you are exploring, prototyping, or want Python-like ease without committing to types just yet.
def quick_test(data):
return len(data)Choose fn for Performance
Pick fn when speed and safety matter: tight loops, library code, and anywhere the compiler should verify your types.
fn fast_add(a: Int, b: Int) -> Int:
return a + bPrototype, Then Harden
A common path is to sketch logic with def, then rewrite the hot parts as fn once the design settles.
Type Safety Trade-off
With def you trade compile-time checks for freedom. With fn you trade freedom for early error detection.
Readability Counts Too
An fn signature reads like documentation: its types tell the next person exactly what goes in and what comes out.
fn distance(x: Float64, y: Float64) -> Float64:
return x * x + y * yMix Both Freely
You can call an fn from a def and the other way around. They live happily together in the same Mojo file.
Interop Often Wants def
When juggling dynamic Python objects through interop, a def can be smoother, since types may not be known upfront.
Hot Loops Want fn
Numeric kernels and inner loops belong in fn, where known types let Mojo generate the tightest possible code.
There Is No Wrong Start
You can always begin with whichever feels natural. Switching a def to an fn later is a small, safe edit.
A Simple Rule of Thumb
If you would benefit from the compiler checking types and squeezing out speed, write fn; otherwise def keeps things light.
Quick Check
Match the situation to the better function style.
Recap
Reach for def when you want flexibility and quick iteration, and fn when you need type safety and speed. 🎯
Frequently asked questions
Is the “When to Reach for Each” lesson free?
Yes — the full text of “When to Reach for Each” 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 “When to Reach for Each”?
Match the function style to your goal. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “When to Reach for Each” 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