Function Arguments
Variable Arguments
Programming
Function `Parameters`
Computer Science

Can a variable number of arguments be passed to a function?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Yes. Many languages let a function accept a variable number of arguments, but the exact mechanism depends on the language. The common idea is the same: instead of a fixed parameter list only, the function receives the "extra" arguments as some collection such as a tuple, array, or argument object. The important design question is not just whether it is possible, but whether it makes the API clearer or harder to use.

Python: *args and **kwargs

Python has the cleanest built-in syntax for this. *args collects extra positional arguments into a tuple, and **kwargs collects extra named arguments into a dictionary.

python
1def describe_user(name, *roles, **options):
2    print("name:", name)
3    print("roles:", roles)
4    print("options:", options)
5
6
7describe_user("Alice", "admin", "editor", active=True, team="platform")

This is flexible and widely used, especially in wrappers and decorators. The downside is that callers may have less guidance if you accept "anything" without strong validation.

JavaScript: rest parameters

Modern JavaScript uses rest parameters. They collect extra arguments into an array.

javascript
1function sum(label, ...numbers) {
2  const total = numbers.reduce((acc, n) => acc + n, 0);
3  console.log(label, total);
4}
5
6sum("total:", 10, 20, 30);

This is preferable to the older arguments object in new code because the rest parameter is explicit, array-based, and easier to type and reason about.

C#: params

C# supports a variable argument list through the params keyword. The extra arguments arrive as an array.

csharp
1using System;
2
3public static class Demo
4{
5    public static int Sum(params int[] values)
6    {
7        int total = 0;
8        foreach (var value in values)
9        {
10            total += value;
11        }
12        return total;
13    }
14
15    public static void Main()
16    {
17        Console.WriteLine(Sum(1, 2, 3, 4));
18    }
19}

This is common for APIs such as string.Format or logging methods. It keeps the call site clean, but it should be used when the repeated values truly belong together as one conceptual parameter list.

Not every language does it the same way

The implementation details differ:

  • Python stores extra positional values in a tuple
  • JavaScript stores them in an array
  • C# uses an array created from the extra arguments
  • C uses low-level variadic functions with stdarg.h, which are much less type-safe

So the answer to the question is almost always "yes, in some form," but the ergonomics and safety vary a lot by language.

When variadic functions are a good idea

They work best when:

  • the extra arguments are all the same conceptual kind
  • the function naturally combines them
  • the caller benefits from a shorter call site

Examples include summing numbers, formatting messages, or building query filters from several conditions.

They are a poor fit when the extra values are really different pieces of required structured data. In that case, a single object or configuration parameter is often clearer.

Common Pitfalls

The biggest mistake is using variadic arguments to avoid designing a real API. A function that accepts "anything" can become hard to document and validate.

Another issue is mixing too many meanings into the same flexible parameter list. That often leads to confusing call sites and complicated runtime checks.

Performance can also matter. In some languages, collecting the extra arguments into an array or tuple allocates new objects, which may be relevant in hot paths.

Finally, do not assume every language supports named and positional variadic arguments in the same way. The feature exists widely, but the syntax and constraints are language-specific.

Summary

  • Many languages support a variable number of arguments, but the syntax differs.
  • Python uses *args and **kwargs.
  • JavaScript uses rest parameters such as ...numbers.
  • C# uses params to gather extra arguments into an array.
  • Variadic functions are best when the extra arguments are naturally one repeated kind of input.

Course illustration
Course illustration

All Rights Reserved.