Skip to content

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.

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 here
fn 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.

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 0

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:

3

If 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.

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

  • 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.

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

deinit 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, self isn’t counted: the message says member function expected ....
  • std.debug.print always needs a second argument, .{} if empty.
  • ArrayList and the hash maps take the allocator in append, deinit and 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

1. What is wrong with std.debug.print("Done\n");?
2. list.deinit() gives member function expected 1 argument(s), found 0. What is missing?
3. How do you give a Zig function an "optional" setting?