liveBook · Manning

Void type

As explained in Systems Programming with Zig

Void is a type representing the absence of a return value.

In Zig, `void` is a function return type for functions that produce no value. A function returning `void` need not return a value; other functions must return one matching their declared type. section 2.1.3

The distinction appears in function declarations: `main` can return `void`, while `fibonacci` returns `i32`. The `!void` return type allows `main` to return no value or report an error. section 1.6.2 section 2.1.3

Listing 1.2 All your codebase still belong to us
const std = @import("std");

pub fn main(init: std.process.Init) !void { #1
    const io = init.io;

    const stdout = std.Io.File.stdout(); #2
    var stdout_buffer: [1024]u8 = undefined;
    var stdout_writer = stdout.writer(io, &stdout_buffer); #3
    const writer = &stdout_writer.interface; #3

    try writer.print("All your codebase still belong to us!\n", .{}); #4
    try writer.flush(); #5
}

See `main` declared with `!void` and how its body handles potentially failing output operations. section 1.6.2

How Systems Programming with Zig covers it

How deep it goes
Shows void return declarations and contrasts them with value-returning functions, without examining representation or internals.
Examples
Uses `main` returning `void` or `!void` and contrasts it with `fibonacci` returning `i32`.
What you can do after
Understand when a Zig function may omit a returned value and how void appears in a declaration.
How it connects
Connects void return types to function declarations and error unions.

Also explained in

Compare how 2 books explain void type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Systems Programming with Zig. The section links point to the parts of the book it draws on.