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.
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.
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.
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
*argsand**kwargs. - JavaScript uses rest parameters such as
...numbers. - C# uses
paramsto gather extra arguments into an array. - Variadic functions are best when the extra arguments are naturally one repeated kind of input.

