Variadic function
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.
The term variadic is a neologism, dating back to 1936. The term was not widely used until the 1970s.
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 logically apply to any number of operands.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, 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 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 to the point where they are not variable-length - 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. 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 can have three variables, one of them a hedge, thus allowing the term to take three or more arguments.
Examples
In C
To portably implement variadic functions in the C programming 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.- include
- include
int main
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. 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. 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.
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 that will be manipulated by the macros.
- takes two arguments, a object and a reference to the function's last parameter. It initialises the object for use by or. The compiler will normally issue a warning if the reference is incorrect, but will not prevent compilation from completing normally.
- takes two arguments, a object 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 you wanted to, for instance, scan the variable arguments more than once, you would re-initialise your object by invoking and then again on it.
- takes two arguments, both of them objects. It clones the second 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 , you could scan the variable arguments a second time with and the second. Don't forget to the clone.
In C#
using System;
class Program
In C++
- include
- include
auto adder
int main
In Go
Variadic functions can be called with any number of trailing arguments. For example, fmt.Println is a common variadic function.package main
import "fmt"
// This variadic function takes an arbitrary number of ints as arguments.
func sum
func main
Output:
The sum of is 3
The sum of is 6
The sum of is 10
In Java
public class Program
In JavaScript
function sum
sum // 6
sum // 5
In PHP
function sum
echo sum; // 6
In Python
def foo:
print # args is a tuple.
foo #
foo #
foo #
In 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 and they flatten arguments by dissolving one or more layers of elements that can be iterated over.
foo #
foo #
foo #
foo; #
- Unflattened slurpy. 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:
bar; #
bar; #
bar; #
bar; # , "'', 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 **@
.sub zaz
zaz; #
zaz; #
zaz; #
zaz; # , single argurment fills up array
zaz; # , 6, behaving as **@
In Ruby
def foo
print args
end
foo
- prints `=> nil`
- prints `=> nil`
In Swift
func greet
greet
// Output:
// Looks like we have 4 people
// Hello Joseph, [good morning
// Hello Clara, good morning
// Hello William, good morning
// Hello Maria, good morning