pub Functions and Visibility
Expose functions across files.
pub Functions and Visibility is a free Zig 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 Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Files Are Modules
In Zig every source file is its own module. What it shares with other files is controlled by visibility rules. 📦
Private by Default
A plain function is private to its file. Other files cannot call it, which keeps internal helpers safely hidden.
fn helper() void {
// only this file can call me
}pub Makes It Public
Add the pub keyword to expose a function so other files can import and call it across your project.
pub fn greet() void {
std.debug.print("Hi!\n", .{});
}Import Brings It In
Another file uses @import to load your module, then reaches public names through the returned value.
const utils = @import("utils.zig");
utils.greet();Only pub Names Are Reachable
Through that import you can call only the public names. Private functions stay invisible, so they cannot be misused.
Why main Needs pub
This is why main is written pub: the runtime lives outside your file and must be able to reach the entry point.
pub Works on More Than fn
The pub keyword also exposes constants, types, and variables, letting you publish a clean public surface for a module.
pub const version = "1.0.0";Design a Small API
Mark only what callers truly need as public. Keeping helpers private gives you a small, stable interface to maintain.
Encapsulation Reduces Bugs
Hiding internals is encapsulation. With fewer exposed parts, you can change implementation details without breaking other files.
No Headers Needed
Unlike C, Zig needs no header files. Importing a file gives you its public declarations directly, with no duplication.
Visibility Is Compile-Time
Zig enforces visibility at compile time. Calling a private function from outside its file simply fails to compile.
Quick Check
You wrote a function in math.zig and another file cannot call it. What is most likely missing?
Recap
Functions are private to their file until you add pub. Expose only what callers need, import with @import, and skip headers entirely. 🎯
Frequently asked questions
Is the “pub Functions and Visibility” lesson free?
Yes — the full text of “pub Functions and Visibility” 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 “pub Functions and Visibility”?
Expose functions across files. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “pub Functions and Visibility” 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