Mutating in Place with inout
Let a function change the caller's value.
Mutating in Place with inout 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.
Sometimes You Must Change It
Borrowing only lets you read. But sometimes a function needs to actually modify the caller's value, like bumping a counter. ✏️
Meet inout
The inout convention gives a function mutable access. Changes you make inside are reflected back in the caller's variable.
In and Out
The name says it all: the value goes in, you change it, and the updated value comes back out to the caller. No copy is returned.
Marking a Parameter
You write inout before the parameter name. Now assignments inside the function update the original variable directly.
fn increment(inout n: Int):
n += 1Calling It
Pass a variable and watch it change after the call. The function edited the caller's value in place, no return needed.
var count = 5
increment(count)
print(count) # 6Must Be Mutable
The argument you pass to an inout parameter must be a mutable var. You cannot hand it a fixed literal or a borrowed value.
Exclusive While Borrowed
While a function holds an inout reference, nothing else may touch that value. This exclusivity rule prevents conflicting edits.
No Hidden Copies
Like borrowing, inout works by reference, so there is no full copy. You get mutation without paying for duplication.
Caller Still Owns It
With inout the caller keeps ownership. The function only borrows mutably for the duration of the call, then hands control back.
When to Choose inout
Use inout when a function's whole job is to update a value the caller will keep using, such as growing a buffer or resetting a field.
Read vs Mutate, Side by Side
So far: borrowed means read-only, inout means read and write. Picking the right one tells readers exactly what a function does.
Quick Check
Let us check what inout enables.
Recap
inout lets a function mutate the caller's value in place, by reference and with no copy. The caller still owns it the whole time. 🎯
Frequently asked questions
Is the “Mutating in Place with inout” lesson free?
Yes — the full text of “Mutating in Place with inout” 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 “Mutating in Place with inout”?
Let a function change the caller's value. 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 “Mutating in Place with inout” 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
- Borrowing Arguments Read-Only
- Mutating in Place with inout
- Taking Ownership with owned
- The Transfer Operator