Functions
functions.zig
const assert = @import("std").debug.assert;
// Functions are declared like this
fn add(a: i8, b: i8) i8 {
if (a == 0) {
return b;
}
return a + b;
}
// The export specifier makes a function externally visible in the generated
// object file, and makes it use the C ABI.
export fn sub(a: i8, b: i8) i8 { return a - b; }
// The extern specifier is used to declare a function that will be resolved
// at link time, when linking statically, or at runtime, when linking
// dynamically.
// The stdcallcc specifier changes the calling convention of the function.
extern "kernel32" stdcallcc fn ExitProcess(exit_code: u32) noreturn;
extern "c" fn atan2(a: f64, b: f64) f64;
// The @setCold builtin tells the optimizer that a function is rarely called.
fn abort() noreturn {
@setCold(true);
while (true) {}
}
// The nakedcc specifier makes a function not have any function prologue or epilogue.
// This can be useful when integrating with assembly.
nakedcc fn _start() noreturn {
abort();
}
// The inline specifier forces a function to be inlined at all call sites.
// If the function cannot be inlined, it is a compile-time error.
inline fn shiftLeftOne(a: u32) u32 {
return a << 1;
}
// The pub specifier allows the function to be visible when importing.
// Another file can use @import and call sub2
pub fn sub2(a: i8, b: i8) i8 { return a - b; }
// Functions can be used as values and are equivalent to pointers.
const call2_op = fn (a: i8, b: i8) i8;
fn do_op(fn_call: call2_op, op1: i8, op2: i8) i8 {
return fn_call(op1, op2);
}
test "function" {
assert(do_op(add, 5, 6) == 11);
assert(do_op(sub2, 5, 6) == -1);
}
$ zig test functions.zig
Test 1/1 function...OK
All tests passed.
Function values are like pointers:
test.zig
const assert = @import("std").debug.assert;
comptime {
assert(@typeOf(foo) == fn()void);
assert(@sizeOf(fn()void) == @sizeOf(?fn()void));
}
fn foo() void { }
$ zig build-obj test.zig
Pass-by-value Parameters
Primitive types such as Integers and Floats passed as parameters are copied, and then the copy is available in the function body. This is called "passing by value". Copying a primitive type is essentially free and typically involves nothing more than setting a register.
Structs, unions, and arrays can sometimes be more efficiently passed as a reference, since a copy could be arbitrarily expensive depending on the size. When these types are passed as parameters, Zig may choose to copy and pass by value, or pass by reference, whichever way Zig decides will be faster. This is made possible, in part, by the fact that parameters are immutable.
test.zig
const Point = struct {
x: i32,
y: i32,
};
fn foo(point: Point) i32 {
// Here, `point` could be a reference, or a copy. The function body
// can ignore the difference and treat it as a value. Be very careful
// taking the address of the parameter - it should be treated as if
// the address will become invalid when the function returns.
return point.x + point.y;
}
const assert = @import("std").debug.assert;
test "pass struct to function" {
assert(foo(Point{ .x = 1, .y = 2 }) == 3);
}
$ zig test test.zig
Test 1/1 pass struct to function...OK
All tests passed.
For extern functions, Zig follows the C ABI for passing structs and unions by value.
Function Reflection
test.zig
const assert = @import("std").debug.assert;
test "fn reflection" {
assert(@typeOf(assert).ReturnType == void);
assert(@typeOf(assert).is_var_args == false);
}
$ zig test test.zig
Test 1/1 fn reflection...OK
All tests passed.