In mathematics and in computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages.
Overview
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 (Common Lisp)| 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 , 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.
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 ; if instances of are a final return value and a function , this allows for any number of additional arguments .
A related subject in term rewriting research is called hedges, or hedge variables. 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).
Examples
C
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 . In C++, the header file is used.
<syntaxhighlight lang="C">
- include <stdarg.h>
- include <stdio.h>
double average(int count, ...) {
va_list ap;
double sum = 0;
va_start(ap, count); // Before C23: Requires the last fixed parameter (to get the address)
for (int j = 0; j < count; ++j) {
sum += va_arg(ap, int); // Increments ap to the next argument
}
va_end(ap);
return sum / count;
}
int main(int argc, char* argv[]) {
printf("%f\n", average(3, 1, 2, 3));
return 0;
}
</syntaxhighlight>
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 , 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 or 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 as a sentinel may encounter such problems; or a dedicated null pointer of the correct target type may be used to avoid them.
declares a type, , and defines four macros: va start|, va arg|, va copy|, and va end|. Each invocation of and must be matched by a corresponding invocation of . When working with variable arguments, a function normally declares a variable of type ( in the example) that will be manipulated by the macros.
- takes two arguments, a object and a reference to the function's last parameter (the one before the ellipsis; the macro uses this to get its bearings). In C23, the second argument will no longer be required and variadic functions will no longer need a named parameter before the ellipsis. It initialises the object for use by or . The compiler will normally issue a warning if the reference is incorrect (e.g. a reference to a different parameter than the last one, or a reference to a wholly different object), but will not prevent compilation from completing normally.
- takes two arguments, a object (previously initialised) and a type descriptor. It expands to the next variable argument, and has the specified type. Successive invocations of allow processing each of the variable arguments in turn. Unspecified behavior occurs if the type is incorrect or there is no next variable argument.
- takes one argument, a object. It serves to clean up. If one wanted to, for instance, scan the variable arguments more than once, the programmer would re-initialise your object by invoking and then again on it.
- takes two arguments, both of them objects. It clones the second (which must have been initialised) into the first. Going back to the "scan the variable arguments more than once" example, this could be achieved by invoking on a first , then using to clone it into a second . After scanning the variable arguments a first time with and the first (disposing of it with ), the programmer could scan the variable arguments a second time with and the second . needs to also be called on the cloned before the containing function returns.
C#
C# describes variadic functions using the keyword. A type must be provided for the arguments, although 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.
<syntaxhighlight lang="c#">
namespace Wikipedia.Examples;
using System;
public class Program
{
static int Foo(int a, int b, params int[] args)
{
// Return the sum of the integers in args, ignoring a and b.
int sum = 0;
foreach (int i in args)
{
sum += i;
}
return sum;
}
static void Main(string[] args)
{
Console.WriteLine(Foo(1, 2)); // 0
Console.WriteLine(Foo(1, 2, 3, 10, 20)); // 33
int[] manyValues = [13, 14, 15];
Console.WriteLine(Foo(1, 2, manyValues)); // 42
}
}
</syntaxhighlight>
C++
The basic variadic facility in C++ is largely identical to that in C. Prior to C++26, the comma before the ellipsis could be omitted. C++ also allows variadic functions without named parameters (for example, <code>void f(...);</code>). However, until C++26 there was no portable way to access those arguments, because <code>va_start</code> required the name of the last fixed parameter of the function. Since C++26, C++ adopts the one-argument form of <code>va_start</code>, matching C23 and allowing access to the arguments even when the function has no fixed parameters.
Variadic templates (parameter pack) can also be used in C++ with language built-in fold expressions. Variadic templates are the only type-safe way to have variadic functions/parameters in C++, as C++ lacks Java-style non-template variadic parameters.
<syntaxhighlight lang="c++">
import std;
template <typename... Ts>
void fooPrint(Ts... args) {
((std::print("{} ", args)), ...);
}
int main(int argc, char* argv[]) {
fooPrint(1, 3.14f); // 1 3.14
fooPrint("Foo", 'b', true, nullptr); // Foo b true nullptr
}
</syntaxhighlight>
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. Variadic templates are the only way to achieve Java-style type-safe variadic parameters.
To constrain the parameter to be only of type <code>T</code> (similar to <code>T... args</code>) in Java, one can use the concept <code>std::same_as</code> or <code>std::convertible_to</code> (for types which may be intended to be convertible).
<syntaxhighlight lang="cpp">
using std::same_as;
// Equivalent to Java declaration
// <T> void fn(T... args)
template <typename T>
void fn(same_as<T> auto... args) {
// ...
}
</syntaxhighlight>
Carbon
Carbon, a language designed with interoperability with C++ in mind, offers variadics, in particular pack expansions.
<syntaxhighlight lang="carbon">
// Takes an arbitrary number of vectors with arbitrary element types, and
// returns a vector of tuples where the i-th element of the vector is
// a tuple of the i-th elements of the input vectors.
fn Zip[... each ElementType:! type](... each vector: Vector(each ElementType)) -> Vector((... each ElementType)) {
... var each iter: auto = each vector.Begin();
var result: Vector((... each ElementType));
while (...and each iter != each vector.End()) {
result.push_back((... each iter));
... each iter++;
}
return result;
}
</syntaxhighlight>
Fortran
Since the Fortran 90 revision, Fortran functions or subroutines can accept optional arguments: the argument list is still fixed, but the ones that have the attribute can be omitted in the function/subroutine call. The intrinsic function can be used to detect the presence of an optional argument. The optional arguments can appear anywhere in the argument list.
<syntaxhighlight lang="fortran">program test
implicit none
real :: x
!> all arguments are passed:
call foo( 1, 2, 3.0, 4, x )
!< outputs 1 \ 2 \ 3.0 \ 4 \ 6.0 (the "\" denotes a newline)
!> the last 2 arguments are omitted:
call foo( 1, 2, 3.0 )
!< outputs 1 \ 2 \ 3.0
!> the 2nd and 4th arguments are omitted: the arguments that are positioned after
!> an omitted argument must be passed with a keyword:
call foo( 1, c=3.0, e=x )
!< outputs 1 \ 3.0 \ 6.0
!> alternatively, the Fortran 2023 revision has introduced the .NIL. pseudo constant
!> to denote an omitted argument
call foo( 1, .NIL., 3.0, .NIL., x )
!< outputs 1 \ 3.0 \ 6.0
contains
!> the subroutine foo() has 2 mandatory and 3 optional arguments
subroutine foo( a, b, c, d, e )
integer, intent(in) :: a
integer, intent(in), optional :: b
real, intent(in) :: c
integer, intent(in), optional :: d
real, intent(out), optional :: e
print*, a
if (present(b)) print*, b
print*, c
if (present(d)) print*, d
if (present(e)) then
e = 2*c
print*, c
end if
end subroutine
end program</syntaxhighlight>
Go
Variadic functions in Go can be called with any number of trailing arguments. is a common variadic function; it uses an empty interface as a catch-all type.
<syntaxhighlight lang="go">
package main
import "fmt"
// This variadic function takes an arbitrary number of ints as arguments.
func sum(nums ...int) {
fmt.Print("The sum of ", nums) // Also a variadic function.
total := 0
for _, num := range nums {
total += num
}
fmt.Println(" is", total) // Also a variadic function.
}
func main() {
// Variadic functions can be called in the usual way with individual
// arguments.
sum(1, 2) // "The sum of [1 2] is 3"
sum(1, 2, 3) // "The sum of [1 2 3] is 6"
// If you already have multiple args in a slice, apply them to a variadic
// function using func(slice...) like this.
nums := []int{1, 2, 3, 4}
sum(nums...) // "The sum of [1 2 3 4] is 10"
}
</syntaxhighlight>
Output:
<pre>
The sum of [1 2] is 3
The sum of [1 2 3] is 6
The sum of [1 2 3 4] is 10
</pre>
Java
As with C#, the type in Java is available as a catch-all.
In Java, a parameter can be variadic using the ellipsis notation. This is essentially equivalent to an array, however it does not require wrapping as an array. For example, <code>String... args</code> and <code>String[] args</code> would be essentially identical.
<syntaxhighlight lang="java">
package org.wikipedia.examples;
public class Program {
// Variadic methods store any additional arguments they receive in an array.
// Consequentially, `printArgs` is actually a method with one parameter: a
// variable-length array of `String`s.
private static void printArgs(String... strings) {
for (String s : strings) {
System.out.println(s);
}
}
public static void main(String[] args) {
printArgs("hello"); // short for printArgs(new String[] {"hello"})
printArgs("hello", "world"); // short for printArgs(new String[] {"hello", "world"})
}
}
</syntaxhighlight>
JavaScript
JavaScript does not care about types of variadic arguments. The variadic argument is denoted with <code>...</code>, which denotes a "rest parameter" which collects the arguments into an array.
<syntaxhighlight lang="javascript">
function sum(...numbers) {
return numbers.reduce((a, b) => a + b, 0);
}
console.log(sum(1, 2, 3)); // 6
console.log(sum(3, 2)); // 5
console.log(sum()); // 0
</syntaxhighlight>
It is also possible to create a variadic function using the arguments object, although it is only usable with functions created with the keyword.
<syntaxhighlight lang="javascript">
function sum() {
return Array.prototype.reduce.call(arguments, (a, b) => a + b, 0);
}
console.log(sum(1, 2, 3)); // 6
console.log(sum(3, 2)); // 5
console.log(sum()); // 0
</syntaxhighlight>
Note that in TypeScript, because the rest parameter is an array, this is denoted as so:
<syntaxhighlight lang="typescript">
function sum(...numbers: number[]): number {
return numbers.reduce((a, b) => a + b, 0);
}
</syntaxhighlight>
TypeScript also allows forcing the rest parameter to have an exact number of elements (i.e. a tuple):
<syntaxhighlight lang="typescript">
// Forces exactly three numbers
function sumExactlyThree(...numbers: [number, number, number]): number {
return numbers.reduce((a, b) => a + b, 0);
}
// Forces at least two numbers
function sumAtLeastTwo(...numbers: [number, number, ...number[]]): number {
return numbers.reduce((a, b) => a + b, 0);
}
// Forces the first argument to be a string, then anything after
function log(...args: [string, ...any[]]) {
// ...
}
</syntaxhighlight>
Lua
Lua functions may pass varargs to other functions the same way as other values using the keyword. tables can be passed into variadic functions by using, in Lua version 5.2 or higher , or Lua 5.1 or lower . Varargs can be used as a table by constructing a table with the vararg as a value.<syntaxhighlight lang="lua">
function sum(...) --... designates varargs
local sum=0
for _,v in pairs({...}) do --creating a table with a varargs is the same as creating one with standard values
sum=sum+v
end
return sum
end
values={1,2,3,4}
sum(5,table.unpack(values)) --returns 15. table.unpack should go after any other arguments, otherwise not all values will be passed into the function.
function add5(...)
return ...+5 --this is incorrect usage of varargs, and will only return the first value provided
end
entries={}
function process_entries()
local processed={}
for i,v in pairs(entries) do
processed[i]=v --placeholder processing code
end
return table.unpack(processed) --returns all entries in a way that can be used as a vararg
end
print(process_entries()) --the print function takes all varargs and writes them to stdout separated by newlines
</syntaxhighlight>
Pascal
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 (/ and /, and additionally in /).
Nonetheless, dialects of Pascal implement mechanisms resembling variadic routines.
Delphi defines an data type that may be associated with the last formal parameter.
Within the routine definition the is an , an array of variant records.
The member of the aforementioned data type allows inspection of the argument’s data type and subsequent appropriate handling.
The Free Pascal Compiler supports Delphi’s variadic routines, too.
This implementation, however, technically requires a single argument, that is an .
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.
Both GNU Pascal and FreePascal allow externally declared functions to use a variadic formal parameter specification using an ellipsis ().
PHP
PHP does not care about types of variadic arguments unless the argument is typed.
<syntaxhighlight lang="php">
function sum(...$nums): int
{
return array_sum($nums);
}
echo sum(1, 2, 3); // 6
</syntaxhighlight>
And typed variadic arguments:
<syntaxhighlight lang="php">
function sum(int ...$nums): int
{
return array_sum($nums);
}
echo sum(1, "a", 3); // TypeError: Argument 2 passed to sum() must be of the type int (since PHP 7.3)
</syntaxhighlight>
Python
Python does not care about types of variadic arguments.
<syntaxhighlight lang="python">
from typing import Any
def foo(a: Any, b: Any, *args: Any]) -> None:
print(args) # args is a tuple (immutable sequence).
if __name__ == "__main__":
foo(1, 2) # args = ()
foo(1, 2, 3) # arga = (3,)
foo(1, 2, 3, "hello") # args = (3, "hello")
</syntaxhighlight>
Keyword arguments can be stored in a dictionary.
<syntaxhighlight lang="python">
def bar(*args: Any, **kwargs: Any]) -> Any:
- function body
</syntaxhighlight>
Note that even though has type ,
is annotated with
.
Raku
In Raku, the type of parameters that create variadic functions are known as slurpy array parameters and they're classified into three groups:
Flattened slurpy
These parameters are declared with a single asterisk (<code>*</code>) and they flatten arguments by dissolving one or more layers of elements that can be iterated over (i.e, Iterables).
<syntaxhighlight lang="raku">
sub foo($a, $b, *@args) {
say @args.perl;
}
foo(1, 2) # []
foo(1, 2, 3) # [3]
foo(1, 2, 3, "hello") # [3 "hello"]
foo(1, 2, 3, [4, 5], [6]); # [3, 4, 5, 6]
</syntaxhighlight>
Unflattened slurpy
These parameters are declared with two asterisks (<code>**</code>) and they do not flatten any iterable arguments within the list, but keep the arguments more or less as-is:
<syntaxhighlight lang="raku">
sub bar($a, $b, **@args) {
say @args.perl;
}
bar(1, 2); # []
bar(1, 2, 3); # [3]
bar(1, 2, 3, "hello"); # [3 "hello"]
bar(1, 2, 3, [4, 5], [6]); # [3, [4, 5], [6]]
</syntaxhighlight>
Contextual slurpy
These parameters are declared with a plus (<code>+</code>) 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, <code>+@</code> works like <code>**@</code> (i.e., unflattened slurpy).
<syntaxhighlight lang="raku">
sub zaz($a, $b, +@args) {
say @args.perl;
}
zaz(1, 2); # []
zaz(1, 2, 3); # [3]
zaz(1, 2, 3, "hello"); # [3 "hello"]
zaz(1, 2, [4, 5]); # [4, 5], single argument fills up array
zaz(1, 2, 3, [4, 5]); # [3, [4, 5]], behaving as **@
zaz(1, 2, 3, [4, 5], [6]); # [3, [4, 5], [6]], behaving as **@
</syntaxhighlight>
Ruby
Ruby does not care about types of variadic arguments.
<syntaxhighlight lang="ruby">
def foo(*args)
print args
end
foo(1)
- prints `[1]=> nil`
foo(1, 2)
- prints `[1, 2]=> nil`
</syntaxhighlight>
Rust
Rust does not support variadic arguments in functions. Instead, it uses macros, which support variadic arguments. This is essentially why <code>println!</code> is a macro and not a function, as it takes variadic arguments to format.
<syntaxhighlight lang="rust">
macro_rules! calculate {
// The pattern for a single `eval`
(eval $e:expr) => ;
// Decompose multiple `eval`s recursively
(eval $e:expr, $(eval $es:expr),+) => ;
}
fn main() {
calculate! {\
eval 1 + 2,
eval 3 + 4,
eval (2 * 3) + 1
}
}
</syntaxhighlight>
Rust is able to interact with C's variadic system via a feature switch. As with other C interfaces, the system is considered to Rust.
<syntaxhighlight lang="rust">
- ![feature(c_variadic)]
pub unsafe extern "C" fn add(n: usize, mut args: ...) -> usize {
let mut sum = 0;
for _ in 0..n {
sum += args.arg::<usize>();
}
sum
}
</syntaxhighlight>
Scala
<syntaxhighlight lang="scala">
object Program {
// Variadic methods store any additional arguments they receive in an array.
// Consequentially, `printArgs` is actually a method with one parameter: a
// variable-length array of `String`s.
private def printArgs(strings: String*): Unit = {
strings.foreach(println)
}
def main(args: Array[String]): Unit = {
printArgs("hello"); // short for printArgs(["hello"])
printArgs("hello", "world"); // short for printArgs(["hello", "world"])
}
}
</syntaxhighlight>
Swift
Swift cares about the type of variadic arguments, but the catch-all type is available.
<syntaxhighlight lang="swift">
func greet(timeOfTheDay: String, names: String...) {
// here, names is [String]
print("Looks like we have \(names.count) people")
for name in names {
print("Hello \(name), good \(timeOfTheDay)")
}
}
greet(timeOfTheDay: "morning", names: "Joseph", "Clara", "William", "Maria")
// Output:
// Looks like we have 4 people
// Hello Joseph, good morning
// Hello Clara, good morning
// Hello William, good morning
// Hello Maria, good morning
</syntaxhighlight>
Tcl
A Tcl procedure or lambda is variadic when its last argument is : this will contain a list (possibly empty) of all the remaining arguments. This pattern is common in many other procedure-like methods.
<syntaxhighlight lang="tcl">
proc greet {timeOfTheDay args} {
puts "Looks like we have [llength $args] people"
foreach name $args {
puts "Hello $name, good $timeOfTheDay"
}
}
greet "morning" "Joseph" "Clara" "William" "Maria"
- Output:
- Looks like we have 4 people
- Hello Joseph, good morning
- Hello Clara, good morning
- Hello William, good morning
- Hello Maria, good morning
</syntaxhighlight>
See also
- Variadic macro
- Variadic template
Notes
References
External links
- Variadic function. Rosetta Code task showing the implementation of variadic functions in over 120 programming languages.
- Variable Argument Functions — A tutorial on Variable Argument Functions for C++
- GNU libc manual
<!-- Hidden categories below -->
