Lesson contents

Functions and scope

Give a calculation a name. Pass in a number, return the result, and use the same function again with a different input.

Simple example

Double three.

Start3 × 2
Result6
lesson.zig
const std = @import("std");

fn double(value: i64) i64 { return value * 2; }

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

Everyday examples

Each bicycle has two wheels. A rack holds seven bicycles. How many wheels are there?

Start7 bikes × 2
Result14
lesson.zig
const std = @import("std");

fn double(value: i64) i64 { return value * 2; }

pub fn main() !void {
    std.debug.print("{any}\n", .{double(7)});
}
Output
14
Run the example
Terminal
zig run lesson.zig
Official documentation