Back to course

Function Pointers and Closures

Full Course: Zig Programming From Zero to Hero

Function Pointers

You can pass functions as arguments using their type signature.

zig const MathOp = fn(i32, i32) i32;

fn execute(op: MathOp, a: i32, b: i32) i32 { return op(a, b); }

Note: Zig doesn't have traditional closures, but you can achieve similar results using structs with data and function pointers.