Zig Error: unused function parameter
error: unused function parameter means a function takes an input it never reads. Zig treats that the same way as an unused local variable: it stops the build. Usually it’s a sign the function isn’t finished, or that the parameter isn’t needed. Sometimes, though, the parameter has to be there (a callback with a fixed shape), and then you name it _. For the other errors in this series, see Zig Errors Explained.
The error message
Section titled “The error message”const std = @import("std");
fn greet(name: []const u8) void { std.debug.print("Hello!\n", .{});}
pub fn main() void { greet("Ada");}main.zig:3:10: error: unused function parameterfn greet(name: []const u8) void { ^~~~The caret points at the parameter’s name in the function’s signature, not at the call.
What it means
Section titled “What it means”A parameter is a name in a function’s signature, like name in fn greet(name: []const u8). The caller has to pass a value for it. If the function never reads that value, either the function has a bug (it was meant to use it) or the caller is passing something for nothing. Zig won’t guess which, so it asks you to decide. The 0.9.0 release notes explain why unused parameters and locals became errors. Zig Function Parameters covers parameters in general.
How to fix it
Section titled “How to fix it”Fix 1: Use the parameter
Section titled “Fix 1: Use the parameter”Most often you just forgot. greet was meant to greet someone by name:
const std = @import("std");
fn greet(name: []const u8) void { std.debug.print("Hello, {s}!\n", .{name});}
pub fn main() void { greet("Ada");}Output:
Hello, Ada!Fix 2: Remove it
Section titled “Fix 2: Remove it”If the function really doesn’t need the input, take it out of the signature and out of every call:
const std = @import("std");
fn greet() void { std.debug.print("Hello!\n", .{});}
pub fn main() void { greet();}Output:
Hello!If you forget a call, Zig tells you with expected 1 argument(s), found 0, so you can’t miss one.
Fix 3: Name it _ when the shape is fixed
Section titled “Fix 3: Name it _ when the shape is fixed”Sometimes you don’t choose the parameters. A sort function in the standard library calls your compare function with three arguments: a context value, then the two items. If you don’t need the context, you still have to accept it:
const std = @import("std");
fn longerFirst(context: void, a: []const u8, b: []const u8) bool { return a.len > b.len;}
pub fn main() void { var words = [_][]const u8{ "fig", "banana", "kiwi" }; std.mem.sort([]const u8, &words, {}, longerFirst); std.debug.print("{s} {s} {s}\n", .{ words[0], words[1], words[2] });}main.zig:3:16: error: unused function parameterfn longerFirst(context: void, a: []const u8, b: []const u8) bool { ^~~~~~~Write _ instead of the name. The parameter is still there, so the shape still matches, but you’ve told Zig it’s ignored on purpose:
const std = @import("std");
fn longerFirst(_: void, a: []const u8, b: []const u8) bool { return a.len > b.len;}
pub fn main() void { var words = [_][]const u8{ "fig", "banana", "kiwi" }; std.mem.sort([]const u8, &words, {}, longerFirst); std.debug.print("{s} {s} {s}\n", .{ words[0], words[1], words[2] });}Output:
banana kiwi figYou’ll see _: void in compare functions all over Zig code. Zig Sorting explains the context argument.
Fix 4: _ = name; while you’re still writing
Section titled “Fix 4: _ = name; while you’re still writing”If you plan to use the parameter soon, keep the name (it documents what the input is) and discard it inside the body for now:
const std = @import("std");
fn saveScore(player: []const u8, score: u32) void { _ = player; // TODO: store per player std.debug.print("saved {d}\n", .{score});}
pub fn main() void { saveScore("Ada", 42);}Output:
saved 42Remove that line once you use player, or you’ll get pointless discard of local variable.
The main(init) case
Section titled “The main(init) case”A very common version of this error comes from main itself. If you write the “juicy” main that receives init and then don’t use it, Zig complains:
const std = @import("std");
pub fn main(init: std.process.Init) void { std.debug.print("hi\n", .{});}main.zig:3:13: error: unused function parameterpub fn main(init: std.process.Init) void { ^~~~If your program doesn’t need I/O, an allocator or arguments, use the plain pub fn main() void. Zig accepts both shapes. Zig main and init explains what init gives you.
Where you’ll usually see it
Section titled “Where you’ll usually see it”- A function you’re still writing, where only some parameters are used so far.
- Callbacks: compare functions for sorting, handlers with a fixed signature, functions you pass as pointers. Use
_. pub fn main(init: std.process.Init)in a program that doesn’t useinityet.- After a refactor that moved the parameter’s job somewhere else.
Related errors
Section titled “Related errors”- unused local variable: the same rule for names inside the function.
- pointless discard of local variable: you kept
_ = x;after usingx. - expected N argument(s), found M: what you see at the call sites after removing a parameter.
- All Zig error pages: the full list.
Practice
Section titled “Practice”1. Area with a label
Section titled “1. Area with a label”printArea should print kitchen: 12. Fix the error by using the parameter, not by removing it.
const std = @import("std");
fn printArea(label: []const u8, w: u32, h: u32) void { std.debug.print("area: {d}\n", .{w * h});}
pub fn main() void { printArea("kitchen", 3, 4);}Hint: {s} prints a string.
Show solution
const std = @import("std");
fn printArea(label: []const u8, w: u32, h: u32) void { std.debug.print("{s}: {d}\n", .{ label, w * h });}
pub fn main() void { printArea("kitchen", 3, 4);}Output:
kitchen: 12The caller already passes a label, so the function should show it. Using the parameter fixes the error and the missing output at the same time.
- Every parameter must be read somewhere in the function body.
- Usually the fix is to use it, or remove it from the signature and every call.
- When the signature is fixed (callbacks, compare functions), name it
_:fn f(_: void, ...). _ = name;in the body is a temporary placeholder; delete it once you usename.- If
maindoesn’t useinit, writepub fn main() void.
Next up: pointless discard of local variable, the error for a leftover _ = x;.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- Functions in the Zig language reference: how parameters are declared and passed.
- Ignoring values in the Zig language reference: how
_discards values. - Compile errors for unused locals (0.9.0 release notes): the change that made unused parameters an error, and the
_ =way out. - Zig Function Parameters: our lesson on parameters, pointers and
anytype. - Zig Sorting: compare functions and the context argument.