Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Zig Dictionary
  3. inline fn / Function Pointers

inline fn / Function Pointers

In Zig, you can use the inline fn keyword to inline a function at its call sites. Zig also supports function pointers, which let you assign a function to a variable or pass it as an argument. This page explains inline functions and how to treat functions as values.

Syntax

// -----------------------------------------------
// inline fn — inlines a function at each call site
// -----------------------------------------------

inline fn functionName(paramName: ParamType) ReturnType {
    return expression;
}
// inline fn expands the function body at each call site
// Use it when you want to eliminate function call overhead

// -----------------------------------------------
// Function pointer type — treat a function as a value
// -----------------------------------------------

// How to write a function pointer type
const varName: *const fn(ParamType) ReturnType = &functionName;

// Higher-order function that receives a function pointer as an argument
fn higherOrderFn(func: *const fn(ParamType) ReturnType, param: ParamType) ReturnType {
    return func(param);
}

// -----------------------------------------------
// comptime function pointer — resolves the type at compile time
// -----------------------------------------------

fn functionName(comptime func: fn(ParamType) ReturnType, param: ParamType) ReturnType {
    return func(param);
}

Syntax Reference

Syntax / KeywordDescription
inline fn name() Type { }Inlines the function at each call site. The function body is embedded directly, eliminating call overhead.
*const fn(Type) TypeRepresents a function pointer type. Allows a function to be assigned to a variable or passed as an argument.
&functionNameObtains a reference (function pointer) to a function. Used when assigning to a variable or passing as an argument.
func(args)Calls a function through a function pointer. The syntax is identical to a normal function call.
comptime func: fn(...) ...Declares a parameter whose function is resolved at compile time. Often combined with inlining.
callconv(.Inline)Explicitly specifies the calling convention for a function. Has the same effect as inline fn.

Sample Code

inline_function.zig
// inline_function.zig — examples of inline fn and function pointers
// Uses characters from Yakuza (Ryū ga Gotoku) to demonstrate
// inlining and treating functions as values

// Import the standard library
const std = @import("std");

// -----------------------------------------------
// inline fn — a function that is inlined at each call site
// The function body is embedded directly at the point of the call
// Best suited for short or frequently-called operations
// -----------------------------------------------

// Boosts Kiryu Kazuma's combat power with the "Dragon Soul" modifier (inline function)
inline fn kiryu_boost(power: u32) u32 {
    // A simple multiplication is a good fit for inline fn
    return power * 3;
}

// Adds the "Mad Dog" modifier to Majima Goro's movement speed (inline function)
inline fn majima_speed(base: u32) u32 {
    return base + 50;
}

// -----------------------------------------------
// Regular functions (defined to be passed as function pointers)
// -----------------------------------------------

// A function that applies Sawamura Haruka's cheering bonus
fn haruka_cheer(power: u32) u32 {
    // Adds +100 as a cheering bonus
    return power + 100;
}

// A function that applies Nishikiyama Akira's combo bonus
fn nishiki_combo(power: u32) u32 {
    // Doubles the value as a combo bonus
    return power * 2;
}

// A function that converts Date Toshio's detective skill into defense
fn date_defense(power: u32) u32 {
    // Returns half the value as defense
    return power / 2;
}

// -----------------------------------------------
// Higher-order function — receives a function pointer as an argument
// *const fn(u32) u32 is the function pointer type syntax
// -----------------------------------------------
fn applySkill(
    name: []const u8,
    base_power: u32,
    skill: *const fn (u32) u32, // takes a function pointer as an argument
) u32 {
    const result = skill(base_power); // calls through the function pointer
    const stdout = std.io.getStdOut().writer();
    stdout.print("  {s}: base {d} → after skill {d}\n", .{ name, base_power, result }) catch {};
    return result;
}

// -----------------------------------------------
// Program entry point
// -----------------------------------------------
pub fn main() !void {
    const stdout = std.io.getStdOut().writer();

    try stdout.print("=== Yakuza Skill System ===\n\n", .{});

    // ① Example of using inline fn
    // kiryu_boost() is expanded inline at the call site
    try stdout.print("--- inline fn demo ---\n", .{});

    const kiryu_base: u32 = 500;
    const kiryu_powered = kiryu_boost(kiryu_base); // inlined here
    try stdout.print("  Kiryu Kazuma: base {d} → after Dragon Soul {d}\n", .{ kiryu_base, kiryu_powered });

    const majima_base: u32 = 420;
    const majima_powered = majima_speed(majima_base); // inlined here
    try stdout.print("  Majima Goro: base {d} → after Mad Dog {d}\n\n", .{ majima_base, majima_powered });

    // ② Example of assigning a function pointer to a variable
    // *const fn(u32) u32 is the function pointer type
    try stdout.print("--- function pointer demo ---\n", .{});

    // Use &functionName to get a function pointer and assign it to a variable
    const skill_haruka: *const fn (u32) u32 = &haruka_cheer;
    const haruka_result = skill_haruka(300); // calls through the function pointer
    try stdout.print("  Sawamura Haruka (via function pointer): 300 → {d}\n\n", .{haruka_result});

    // ③ Example of storing function pointers in an array and switching between them
    // Three characters' skill functions are placed in an array
    try stdout.print("--- function pointer array demo ---\n", .{});

    const names = [_][]const u8{ "Sawamura Haruka", "Nishikiyama Akira", "Date Toshio" };
    const skills = [_]*const fn (u32) u32{ &haruka_cheer, &nishiki_combo, &date_defense };
    const base_powers = [_]u32{ 300, 480, 260 };

    // Call each skill function in order using a for loop
    for (names, skills, base_powers) |name, skill, base| {
        const result = skill(base); // calls the function pointer stored in the array
        try stdout.print("  {s}: {d} → {d}\n", .{ name, base, result });
    }

    try stdout.print("\n", .{});

    // ④ Example of passing a function pointer to a higher-order function
    // Pass &functionName as the third argument to applySkill()
    try stdout.print("--- higher-order function demo ---\n", .{});
    _ = applySkill("Kiryu Kazuma (cheer bonus)", kiryu_base, &haruka_cheer);
    _ = applySkill("Nishikiyama Akira (combo bonus)", 480, &nishiki_combo);
    _ = applySkill("Date Toshio (defense conversion)", 260, &date_defense);
}
zig run inline_function.zig
=== Yakuza Skill System ===

--- inline fn demo ---
  Kiryu Kazuma: base 500 → after Dragon Soul 1500
  Majima Goro: base 420 → after Mad Dog 470

--- function pointer demo ---
  Sawamura Haruka (via function pointer): 300 → 400

--- function pointer array demo ---
  Sawamura Haruka: 300 → 400
  Nishikiyama Akira: 480 → 960
  Date Toshio: 260 → 130

--- higher-order function demo ---
  Kiryu Kazuma (cheer bonus): base 500 → after skill 600
  Nishikiyama Akira (combo bonus): base 480 → after skill 960
  Date Toshio (defense conversion): base 260 → after skill 130

Overview

The inline fn keyword in Zig instructs the compiler to expand the function body directly at each call site. A normal function call incurs overhead from stack frame creation and argument passing, but inline fn eliminates that cost. It is well suited for short arithmetic operations like the kiryu_boost() and majima_speed() functions in the sample. A function pointer type is written as *const fn(ParamType) ReturnType, and a function pointer is obtained with &functionName. The pointer can be assigned to a variable, passed as an argument to a higher-order function like applySkill(), or stored in an array to switch which function is called at runtime. For the basics of functions, see Function Basics. For functions that return errors, see Functions That Return Errors.

If you find any errors or copyright issues, please .