Borrowing Arguments Read-Only
The borrowed default for safe reads.
Borrowing Arguments Read-Only is a free Mojo Academy lesson on CoddyKit — lesson 1 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.
Why Ownership Matters
When you pass a value to a function, Mojo needs to know who can read or change it. That rule is an ownership convention. 🔑
The Default Is Borrowing
By default an argument is borrowed. The function gets read access to the caller's value but does not take it over.
Read, Don't Modify
A borrowed argument is read-only inside the function. You can look at it freely, but Mojo will not let you change it.
No Copy Needed
Borrowing avoids copying the data. The function shares the caller's value as an immutable reference, which keeps things fast.
Spelling It Out
You can write borrowed before a parameter to be explicit, though it is already the default for an fn function.
fn area(borrowed w: Int, borrowed h: Int) -> Int:
return w * hThe Same Without the Keyword
Because borrowing is the default, you usually skip the keyword. This function reads its argument exactly the same way.
fn area(w: Int, h: Int) -> Int:
return w * hTrying to Mutate Fails
If you assign to a borrowed argument, the compiler stops you. That error is Mojo protecting the caller's value.
Safe to Share
Because the function only reads, you can borrow the same value in many calls at once with no risk of a surprise change.
Great for Big Data
Borrowing shines when a value is large. You read a big struct without paying the cost of a full copy on every call.
Caller Keeps Ownership
After a borrowed call, the original variable is still valid and unchanged. The caller never lost ownership of its value.
The Safe Read Default
Borrowing is Mojo's safe, efficient default: read access, no copy, no mutation. Reach for it whenever a function only needs to inspect data.
Quick Check
Let us test what a borrowed argument allows.
Recap
Borrowing is the default: the function reads the caller's value with no copy and no mutation. It is Mojo's safe, fast way to pass data. 🎯
Frequently asked questions
Is the “Borrowing Arguments Read-Only” lesson free?
Yes — the full text of “Borrowing Arguments Read-Only” 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 “Borrowing Arguments Read-Only”?
The borrowed default for safe reads. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Borrowing Arguments Read-Only” 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