Lesson contents

Organizing source files

Put the price calculation in its own file. The main program calls that function. Save both files in the same folder before running the example.

Simple example

Call a function in another file.

Start2 × 3
Result6
prices.zig
pub fn total(units: i64, price: i64) i64 { return units * price; }
lesson.zig
const std = @import("std");
const prices = @import("prices.zig");

pub fn main() !void {
    std.debug.print("{any}\n", .{prices.total(2, 3)});
}
Output
6
Advertisements

Everyday examples

A snack stall sells five juices at two coins each. Use the shared price function to print the amount due.

Start5 juices × 2
Result10
prices.zig
pub fn total(units: i64, price: i64) i64 { return units * price; }
lesson.zig
const std = @import("std");
const prices = @import("prices.zig");

pub fn main() !void {
    std.debug.print("{any}\n", .{prices.total(5, 2)});
}
Output
10
Run the example
Terminal
zig run lesson.zig
Official documentation