There are many mathematical and logical operations that come across naturally as variadic functions. For instance, the summing of numbers or the concatenation of strings or other sequences are operations that can be thought of as applicable to any number of operands (even though formally in these cases the associative property is applied).
Another operation that has been implemented as a variadic function in many languages is output formatting. The C function printf and the Common Lisp function format are two such examples. Both take one argument that specifies the formatting of the output, and any number of arguments that provide the values to be formatted.
Variadic functions can expose type-safety problems in some languages. For instance, C's printf, if used incautiously, can give rise to a class of security holes known as format string attacks. The attack is possible because the language support for variadic functions is not type-safe: it permits the function to attempt to pop more arguments off the stack than were placed there, corrupting the stack and leading to unexpected behavior. As a consequence of this, the CERT Coordination Center considers variadic functions in C to be a high-severity security risk.2
In functional programming languages, variadics can be considered complementary to the apply function, which takes a function and a list/sequence/array as arguments, and calls the function with the arguments supplied in that list, thus passing a variable number of arguments to the function. In the functional language Haskell, variadic functions can be implemented by returning a value of a type class T; if instances of T are a final return value r and a function (T t) => x -> t, this allows for any number of additional arguments x.
A related subject in term rewriting research is called hedges, or hedge variables.3 Unlike variadics, which are functions with arguments, hedges are sequences of arguments themselves. They also can have constraints ('take no more than 4 arguments', for example) to the point where they are not variable-length (such as 'take exactly 4 arguments') - thus calling them variadics can be misleading. However they are referring to the same phenomenon, and sometimes the phrasing is mixed, resulting in names such as variadic variable (synonymous to hedge). Note the double meaning of the word variable and the difference between arguments and variables in functional programming and term rewriting. For example, a term (function) can have three variables, one of them a hedge, thus allowing the term to take three or more arguments (or two or more if the hedge is allowed to be empty).
To portably implement variadic functions in the C language, the standard stdarg.h header file is used. The older varargs.h header has been deprecated in favor of stdarg.h. In C++, the header file cstdarg is used.4
This will compute the average of an arbitrary number of arguments. Note that the function does not know the number of arguments or their types. The above function expects that the types will be int, and that the number of arguments is passed in the first argument (this is a frequent usage but by no means enforced by the language or compiler). In some other cases, for example printf, the number and types of arguments are figured out from a format string. In both cases, this depends on the programmer to supply the correct information. (Alternatively, a sentinel value like NULL or nullptr may be used to indicate the end of the parameter list.) If fewer arguments are passed in than the function believes, or the types of arguments are incorrect, this could cause it to read into invalid areas of memory and can lead to vulnerabilities like the format string attack. Depending on the system, even using NULL as a sentinel may encounter such problems; nullptr or a dedicated null pointer of the correct target type may be used to avoid them.
stdarg.h declares a type, va_list, and defines four macros: va_start, va_arg, va_copy, and va_end. Each invocation of va_start and va_copy must be matched by a corresponding invocation of va_end. When working with variable arguments, a function normally declares a variable of type va_list (ap in the example) that will be manipulated by the macros.
C# describes variadic functions using the params keyword. A type must be provided for the arguments, although object[] can be used as a catch-all. At the calling site, you can either list the arguments one by one, or hand over a pre-existing array having the required element type. Using the variadic form is Syntactic sugar for the latter.
The basic variadic facility in C++ is largely identical to that in C. The only difference is in the syntax, where the comma before the ellipsis can be omitted. C++ allows variadic functions without named parameters but provides no way to access those arguments since va_start requires the name of the last fixed argument of the function.
Variadic templates (parameter pack) can also be used in C++ with language built-in fold expressions.
The CERT Coding Standards for C++ strongly prefers the use of variadic templates (parameter pack) in C++ over the C-style variadic function due to a lower risk of misuse.7
Since the Fortran 90 revision, Fortran functions or subroutines can accept optional arguments:8 the argument list is still fixed, but the ones that have the optional attribute can be omitted in the function/subroutine call. The intrinsic function present() can be used to detect the presence of an optional argument. The optional arguments can appear anywhere in the argument list.
Output:
Variadic functions in Go can be called with any number of trailing arguments.9 fmt.Println is a common variadic function; it uses an empty interface as a catch-all type.
As with C#, the Object type in Java is available as a catch-all.
JavaScript does not care about types of variadic arguments.
It's also possible to create a variadic function using the arguments object, although it is only usable with functions created with the function keyword.
Lua functions may pass varargs to other functions the same way as other values using the return keyword. tables can be passed into variadic functions by using, in Lua version 5.2 or higher10 table.unpack, or Lua 5.1 or lower11 unpack. Varargs can be used as a table by constructing a table with the vararg as a value.
Pascal is standardized by ISO standards 7185 (“Standard Pascal”) and 10206 (“Extended Pascal”). Neither standardized form of Pascal supports variadic routines, except for certain built-in routines (read/readLn and write/writeLn, and additionally in EP readStr/writeStr).
Nonetheless, dialects of Pascal implement mechanisms resembling variadic routines. Delphi defines an array of const data type that may be associated with the last formal parameter. Within the routine definition the array of const is an array of TVarRec, an array of variant records.12 The VType member of the aforementioned record data type allows inspection of the argument’s data type and subsequent appropriate handling. The Free Pascal Compiler supports Delphi’s variadic routines, too.13
This implementation, however, technically requires a single argument, that is an array. Pascal imposes the restriction that arrays need to be homogenous. This requirement is circumvented by utilizing a variant record. The GNU Pascal defines a real variadic formal parameter specification using an ellipsis (...), but as of 2022 no portable mechanism to use such has been defined.14
Both GNU Pascal and FreePascal allow externally declared functions to use a variadic formal parameter specification using an ellipsis (...).
PHP does not care about types of variadic arguments unless the argument is typed.
And typed variadic arguments:
Python does not care about types of variadic arguments.
Keyword arguments can be stored in a dictionary, e.g. def bar(*args, **kwargs).
In Raku, the type of parameters that create variadic functions are known as slurpy array parameters and they're classified into three groups:
These parameters are declared with a single asterisk (*) and they flatten arguments by dissolving one or more layers of elements that can be iterated over (i.e, Iterables).
These parameters are declared with two asterisks (**) and they do not flatten any iterable arguments within the list, but keep the arguments more or less as-is:
These parameters are declared with a plus (+) sign and they apply the "single argument rule", which decides how to handle the slurpy argument based upon context. Simply put, if only a single argument is passed and that argument is iterable, that argument is used to fill the slurpy parameter array. In any other case, +@ works like **@ (i.e., unflattened slurpy).
Ruby does not care about types of variadic arguments.
Rust does not support variadic arguments in functions. Instead, it uses macros.15
Rust is able to interact with C's variadic system via a c_variadic feature switch. As with other C interfaces, the system is considered unsafe to Rust.16
Swift cares about the type of variadic arguments, but the catch-all Any type is available.
A Tcl procedure or lambda is variadic when its last argument is args: this will contain a list (possibly empty) of all the remaining arguments. This pattern is common in many other procedure-like methods.1718
Henry S. Leonard and H. N. Goodman, A calculus of individuals. Abstract of a talk given at the Second Meeting of the Association for Symbolic Logic, held in Cambridge MA on December 28–30, 1936, [1], Journal of Symbolic Logic 2(1) 1937, 63. https://www.jstor.org/stable/2268861 ↩
Klemens, Ben (2014). 21st Century C: C Tips from the New School. O'Reilly Media, Inc. p. 224. ISBN 978-1491904442. 978-1491904442 ↩
CLP (H): Constraint Logic Programming for Hedges https://arxiv.org/abs/1503.00336 ↩
" (stdarg.h) - C++ Reference". www.cplusplus.com. http://www.cplusplus.com/reference/clibrary/cstdarg/ ↩
Making the named parameter optional was needed since there was no way to specify a function taking an unspecified number of arguments in C23 after the removal of K&R style function definitions. Since C++ was already using this syntax for the same purpose, this change was also a way to increase compatibility between the languages.[5] ↩
Gilding, Alex; Meneide, JeanHeyd (2022-04-15). "WG14-N2975 : Relax requirements for variadic parameter lists, v3" (PDF). https://open-std.org/JTC1/SC22/WG14/www/docs/n2975.pdf ↩
"DCL50-CPP. Do not define a C-style variadic function". https://wiki.sei.cmu.edu/confluence/display/cplusplus/DCL50-CPP ↩
"Optional Arguments". Intel. Retrieved 2025-03-18. https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-0/optional-arguments.html ↩
"Go by Example: Variadic Functions". https://gobyexample.com/variadic-functions ↩
"Lua 5.2 Reference Manual". www.lua.org. Retrieved 2023-02-05. https://www.lua.org/manual/5.2/manual.html#pdf-table.unpack ↩
"Lua 5.1 Reference Manual". www.lua.org. Retrieved 2023-02-05. https://www.lua.org/manual/5.1/manual.html#pdf-unpack ↩
"Parameters (Delphi)". Retrieved 2023-08-28. https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Parameters_(Delphi)#Variant_Open_Array_Parameters ↩
"Free Pascal - Reference guide". Retrieved 2023-08-28. https://www.freepascal.org/docs-html/3.2.0/ref/refsu68.html ↩
"The GNU Pascal Manual". Retrieved 2023-08-28. https://www.gnu-pascal.de/gpc/Special-Parameters.html ↩
"Variadics". Rust By Example. https://doc.rust-lang.org/rust-by-example/macros/variadics.html ↩
"2137-variadic". The Rust RFC Book. https://rust-lang.github.io/rfcs/2137-variadic.html ↩
"proc manual page". Tcl/Tk Documentation. https://www.tcl.tk/man/tcl/TclCmd/proc.html ↩
"args". Tcler's Wiki. https://wiki.tcl-lang.org/page/args ↩