Skip to content

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.

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 parameter
fn greet(name: []const u8) void {
^~~~

The caret points at the parameter’s name in the function’s signature, not at the call.

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.

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!

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.

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 parameter
fn 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 fig

You’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 42

Remove that line once you use player, or you’ll get pointless discard of local variable.

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 parameter
pub 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.

  • 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 use init yet.
  • After a refactor that moved the parameter’s job somewhere else.

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: 12

The 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 use name.
  • If main doesn’t use init, write pub fn main() void.

Next up: pointless discard of local variable, the error for a leftover _ = x;.

Quick check

1. Your compare function must accept a context argument, but doesn't need it. What should you write?
2. You remove a parameter from a function. What else must change?
3. pub fn main(init: std.process.Init) void only prints with std.debug.print. What is the simplest fix?