Passing Values vs References
How arguments reach your function.
Passing Values vs References is a free Zig 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 Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
How Arguments Travel
When you call a function, Zig must get your data inside it. There are two routes: pass the value itself or pass a reference to it. 🚚
Passing by Value Copies
By default, primitive arguments are passed by value: the function gets its own copy and cannot change your original.
fn addOne(n: i32) i32 {
return n + 1;
}Copies Stay Independent
Because it is a copy, edits inside the function never touch the caller. Your variable outside stays exactly as it was.
Parameters Are Immutable
Function parameters in Zig are immutable. You cannot reassign a parameter inside the body, which keeps intent clear.
Pass a Pointer to Share
To let a function change your data, pass a pointer with *T. The function then works on your actual value, not a copy.
fn bump(n: *i32) void {
n.* += 1;
}Dereference to Reach It
Inside, you use .* to read or write through the pointer. n.* += 1 updates the value the caller still holds.
Take the Address with &
At the call site you pass an address using &. Then bump(&count) lets the function modify count in place.
var count: i32 = 0;
bump(&count); // count is now 1Big Data Prefers References
For large structs, copying is wasteful. Passing a const pointer shares the data cheaply without allowing changes.
fn area(r: *const Rect) i32 {
return r.w * r.h;
}const Pointer Means Read-Only
A *const T lets the function read your data but never write it. You get speed without giving up safety.
Slices Are Already Views
A slice like []const u8 carries a pointer plus a length, so passing it shares the underlying data without a full copy.
Choose Based on Intent
Pass by value for small, read-only data; pass a pointer when you must mutate or when the data is large.
Quick Check
You want a function to change the caller variable. Which parameter form lets it do that?
Recap
Values are copied and cannot change your originals; pointers share them. Use *T to mutate and *const T to read large data fast. 🎯
Frequently asked questions
Is the “Passing Values vs References” lesson free?
Yes — the full text of “Passing Values vs References” is free to read here on the web, and the Zig 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 Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “Passing Values vs References”?
How arguments reach your function. You practise Zig 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 Zig Academy?
No prior experience is required. Zig 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 “Passing Values vs References” 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 Zig Academy lesson?
Yes. Every Zig 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
- Function Syntax and Return Types
- Passing Values vs References
- pub Functions and Visibility
- Recursion and Multiple Returns