A First Look at Mutation
Changing data without breaking other code.
A First Look at Mutation is a free Mojo Academy lesson on CoddyKit — lesson 4 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.
Changing Data On Purpose
Independent copies are great, but real programs must mutate data: update a score, grow a list. Let us do it safely. ✏️
Mutating Your Own var
The simplest mutation is reassigning a var you own. Since it is your copy, the change affects only your value.
var score = 10
score = score + 5Local Mutation Is Free of Side Effects
Editing a local var cannot leak outward. Other variables keep their values, so the change has no surprise side effects.
var a = 1
var b = a
a = 100Functions Get Copies By Default
Pass a value to a function and it edits a copy. Your original outside the function is left untouched.
fn bump(n: Int) -> Int:
return n + 1But Sometimes You Want Real Change
What if a function should update the caller's value in place? Mojo lets you opt in with the inout convention.
inout Means Mutate In Place
Mark an argument inout and the function works on the caller's actual value, so its edits truly stick.
fn double(inout n: Int):
n = n * 2Mutation Is Explicit, Not Accidental
Notice you had to write inout on purpose. Mojo never mutates the caller behind your back; you always opt in.
Reading the Signature Tells You
The function signature reveals intent. See inout and you know your value may change; otherwise expect a safe copy.
Calling an inout Function
Call it with your var and watch the value update right where it lives. The function reached your original, by design.
var n = 4
double(n)
print(n)A Balanced Approach
Mojo blends both worlds: safe copies by default, deliberate mutation when you ask. You get safety and flexibility together.
What Comes Next
This is just a first taste. Later you will meet owned and the transfer operator for full control over how data moves. 🚀
Quick Check
You want a function to change the caller's actual value, not a copy. What do you use?
Recap
Mutating your own var is safe and local. To change a caller's value, opt in with inout; nothing mutates by accident. 🎯
Frequently asked questions
Is the “A First Look at Mutation” lesson free?
Yes — the full text of “A First Look at Mutation” 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 “A First Look at Mutation”?
Changing data without breaking other code. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “A First Look at Mutation” 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
- What Value Semantics Means
- Copies vs Shared State
- Mojo's Safe Default
- A First Look at Mutation