for Zig 0.15.2 Buy

CHAPTER 7

Functions and Lists


Functions in Zig are comparable to mathematical functions: we pass in some input parameters and return a value using return <returned-value>. To define a function, we use this form:


fn name (  param: type,   … ,   paramN: typeN  ) return_type{

      // … function code

     return value;

     // if the return type is void, we don’t return anything

}; 


It is mandatory to specify the return type, even if it is void (nothing). The value returned by a function must match the type indicated in its declaration. Returning nothing (not writing return) is the same as returning nothing with return;

It is like pouring an empty ladle:

void_ladle.zig

const std = @import("std");

const print = std.debug.print;

fn empty() void {}

fn nothing() void {

    return;

}

pub fn main() void {

    const void_x = empty();

    const void_y = nothing();

    print("Returned empty:{} , nothing:{}\n", .{

        void_x, void_y });

}

$ zig run voidfun.zig

Returned empty:void , nothing:void

Chapter Summary
Parameters and arguments
© 2025 - 2026 Zen of Zig