Zig Error: expected N argument(s), found M
error: expected 2 argument(s), found 1 means you called a function with a different number of arguments than it takes. Zig has no default arguments and no overloading, so every call must pass exactly what the function lists. The numbers change with the function, and for methods the message starts with member function. Three causes come up again and again: a missing .{} in std.debug.print, a missing allocator in append or deinit, and code from an older Zig where the function took different parameters. For the other errors in this series, see Zig Errors Explained.
The error message
Section titled “The error message”const std = @import("std");
fn add(a: i32, b: i32) i32 { return a + b;}
pub fn main() void { std.debug.print("{d}\n", .{add(1)});}main.zig:8:32: error: expected 2 argument(s), found 1 std.debug.print("{d}\n", .{add(1)}); ^~~main.zig:3:1: note: function declared herefn add(a: i32, b: i32) i32 {^~~~~~~~~~~~~~~~~~~~~~~~~~The caret points at the call, and the note shows the function’s signature, so you can count its parameters right there: a and b, two of them.
What it means
Section titled “What it means”In Zig, a call must pass one argument for every parameter, in order. There’s no way to leave one out and get a default, and you can’t write a second add with three parameters next to the first. That keeps every call honest: you can read exactly what goes in without looking anything up. The Functions section of the language reference shows how parameters are declared and passed. Zig Functions covers the basics.
For methods, the count doesn’t include self. list.deinit() is short for deinit(&list), so self is already filled in, and the message counts the rest:
member function expected 1 argument(s), found 0How to fix it
Section titled “How to fix it”Fix 1: Pass every argument
Section titled “Fix 1: Pass every argument”Look at the signature in the note and pass what’s missing:
const std = @import("std");
fn add(a: i32, b: i32) i32 { return a + b;}
pub fn main() void { std.debug.print("{d}\n", .{add(1, 2)});}Output:
3If you have too many, the message says so the other way round (expected 2 argument(s), found 3). Either remove the extra one or add the parameter to the function.
Fix 2: std.debug.print always needs .{}
Section titled “Fix 2: std.debug.print always needs .{}”This one gets everyone on day one:
const std = @import("std");
pub fn main() void { std.debug.print("Hello!\n");}main.zig:4:14: error: expected 2 argument(s), found 1 std.debug.print("Hello!\n"); ~~~~~~~~~^~~~~~.../lib/std/debug.zig:323:5: note: function declared herepub fn print(comptime fmt: []const u8, args: anytype) void {~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~print takes a format string and a list of values to fill in. With nothing to fill in, pass an empty list, .{}:
const std = @import("std");
pub fn main() void { std.debug.print("Hello!\n", .{});}Output:
Hello!The same goes for writer.print(...). Zig Output explains the .{ ... } list.
Fix 3: Pass the allocator to append, deinit and friends
Section titled “Fix 3: Pass the allocator to append, deinit and friends”std.ArrayList and the hash maps don’t store an allocator. You pass it to every call that allocates or frees memory:
const std = @import("std");
pub fn main(init: std.process.Init) !void { var list: std.ArrayList(i32) = .empty; defer list.deinit(); try list.append(init.gpa, 1);}main.zig:5:15: error: member function expected 1 argument(s), found 0 defer list.deinit(); ~~~~^~~~~~~.../lib/std/array_list.zig:690:13: note: function declared here pub fn deinit(self: *Self, gpa: Allocator) void { ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~The note shows it: deinit wants gpa: Allocator after self. Pass the same allocator you used for append:
const std = @import("std");
pub fn main(init: std.process.Init) !void { const gpa = init.gpa; var list: std.ArrayList(i32) = .empty; defer list.deinit(gpa); try list.append(gpa, 1); try list.append(gpa, 2); std.debug.print("{any}\n", .{list.items});}Output:
{ 1, 2 }Older Zig code wrote std.ArrayList(i32).init(allocator) and then list.append(1) and list.deinit(). That style is gone, which is why copied code so often hits this error. Putting the allocator in a const gpa at the top keeps the calls short. Zig ArrayList has the details.
Fix 4: Use an options struct instead of default arguments
Section titled “Fix 4: Use an options struct instead of default arguments”If you wanted an argument to be optional, Zig’s answer is a struct with default field values. The caller passes .{} for “all defaults” and names only what they change:
const std = @import("std");
const GreetOptions = struct { greeting: []const u8 = "Hello", excited: bool = false,};
fn greet(name: []const u8, opts: GreetOptions) void { const end = if (opts.excited) "!" else "."; std.debug.print("{s}, {s}{s}\n", .{ opts.greeting, name, end });}
pub fn main() void { greet("Ada", .{}); greet("Bob", .{ .excited = true }); greet("Cy", .{ .greeting = "Hi" });}Output:
Hello, Ada.Hello, Bob!Hi, Cy.You’ll see this pattern all over the standard library, for example the .{} in std.json.fmt(value, .{}). Zig Function Parameters covers it.
Where you’ll usually see it
Section titled “Where you’ll usually see it”std.debug.print("text\n")without.{}.list.append(x),list.deinit(),map.put(k, v)without the allocator, often in code written for Zig 0.14 or older.- After you added a parameter to your own function and missed some calls.
- Calls copied from other languages that use default or keyword arguments.
Related errors
Section titled “Related errors”- missing struct field: x: the struct version, a field left out of
.{ ... }. - unused function parameter: if a parameter isn’t needed, remove it from the signature and the calls.
- no field or member function named ‘x’: the method you called doesn’t exist at all.
- All Zig error pages: the full list.
Practice
Section titled “Practice”1. Shopping list
Section titled “1. Shopping list”Fix the three calls so this program prints { 2, 5 } and items: 2.
const std = @import("std");
pub fn main(init: std.process.Init) !void { const gpa = init.gpa; var cart: std.ArrayList(u32) = .empty; defer cart.deinit(); try cart.append(2); try cart.append(gpa, 5); std.debug.print("{any}\n", .{cart.items}); std.debug.print("items: {d}\n", cart.items.len);}Hint: two calls are missing the allocator. The last print has the right number of arguments, but the second one isn’t a list.
Show solution
const std = @import("std");
pub fn main(init: std.process.Init) !void { const gpa = init.gpa; var cart: std.ArrayList(u32) = .empty; defer cart.deinit(gpa); try cart.append(gpa, 2); try cart.append(gpa, 5); std.debug.print("{any}\n", .{cart.items}); std.debug.print("items: {d}\n", .{cart.items.len});}Output:
{ 2, 5 }items: 2deinit and append both take the allocator after self. The values for print always go inside .{ ... }, even when there’s only one.
- Every call must pass exactly one argument per parameter. Zig has no default arguments and no overloading.
- The note shows the function’s signature: count its parameters there.
- For methods,
selfisn’t counted: the message saysmember function expected .... std.debug.printalways needs a second argument,.{}if empty.ArrayListand the hash maps take the allocator inappend,deinitand other calls.- For optional settings, use an options struct with default field values.
Next up: missing struct field: x, the error for a struct literal that leaves out a field.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- Functions in the Zig language reference: how parameters and calls work.
- Default field values in the Zig language reference: the options-struct pattern.
- 0.17.0 release notes: changes to
ArrayListand other standard library signatures. - Zig ArrayList: our lesson on lists and passing the allocator.