Values & References2 / 18
Up next

Pass by value copies

const std = @import("std");

// n is a copy; changing it does not touch the caller
fn tryDouble(n: i32) void {
    var local = n;
    local *= 2;
    // local is the doubled copy; the caller's value is untouched
    std.debug.print("inside: {d}\n", .{local});
}

pub fn main() void {
    const x: i32 = 5;
    tryDouble(x);
    // x is unchanged because it was passed by value
    std.debug.print("{d}\n", .{x});
}
What you're solving

a value argument is a copy