Back to course

Functions: Basics and Returning Values

Full Course: Zig Programming From Zero to Hero

Functions in Zig

Functions are declared using the fn keyword. You must specify parameter types and the return type.

zig pub fn add(a: i32, b: i32) i32 { return a + b; }

pub fn main() void { const result = add(5, 10); std.debug.print("Sum: {d}\n", .{result}); }

Functions can return void if they don't return anything. The pub keyword makes the function visible to other files.