Jump to content

Variadic function: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Citation bot (talk | contribs)
Add: date. | Use this bot. Report bugs. | Suggested by Headbomb | Linked from Wikipedia:WikiProject_Academic_Journals/Journals_cited_by_Wikipedia/Sandbox2 | #UCB_webform_linked 222/229
m Reverted edit by 2603:7000:C13C:6B1D:78BA:5624:F2B9:75F3 (talk) to last version by Jarble
(13 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{Short description|Function with variable number of arguments}}
{{Redirect|Varargs|the varargs.h library in C|varargs.h}}
{{Redirect|Varargs|the varargs.h library in C|varargs.h}}


In [[mathematics]] and in [[computer programming]], a '''variadic function''' is a [[function (programming)|function]] of indefinite [[arity]], i.e., one which accepts a variable number of [[argument (computer science)|argument]]s. Support for variadic functions differs widely among [[programming language]]s.
In [[mathematics]] and in [[computer programming]], a '''variadic function''' is a [[function (programming)|function]] of indefinite [[arity]], i.e., one which accepts a variable number of [[argument (computer science)|argument]]s. Support for variadic functions differs widely among [[programming language]]s.


The term ''variadic'' is a [[neologism]], dating back to 1936–1937.<ref>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, [https://www.jstor.org/stable/2268861?seq=1#metadata_info_tab_contents], ''Journal of Symbolic Logic'' '''2'''(1) 1937, 63.</ref> The term was not widely used until the 1970s.
The term ''variadic'' is a [[neologism]], dating back to 1936–1937.<ref>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, [https://www.jstor.org/stable/2268861], ''Journal of Symbolic Logic'' '''2'''(1) 1937, 63.</ref> The term was not widely used until the 1970s.


==Overview==
==Overview==
Line 12: Line 13:
Variadic functions can expose [[Type safety|type-safety]] problems in some languages. For instance, C's {{code|printf}}, if used incautiously, can give rise to a class of security holes known as [[format string attack]]s. 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 (abstract data type)#Hardware stacks|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.<ref>{{cite book|last=Klemens|first=Ben|title=21st Century C: C Tips from the New School|publisher=O'Reilly Media, Inc.|date=2014|pages=224|isbn=978-1491904442}}</ref>
Variadic functions can expose [[Type safety|type-safety]] problems in some languages. For instance, C's {{code|printf}}, if used incautiously, can give rise to a class of security holes known as [[format string attack]]s. 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 (abstract data type)#Hardware stacks|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.<ref>{{cite book|last=Klemens|first=Ben|title=21st Century C: C Tips from the New School|publisher=O'Reilly Media, Inc.|date=2014|pages=224|isbn=978-1491904442}}</ref>


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.{{citation needed|date=February 2018}} In the functional language [[Haskell (programming language)|Haskell]], variadic functions can be implemented by returning a value of a [[type class]] {{code|T}}; if instances of {{code|T}} are a final return value {{code|r}} and a function {{code|1=(T t) => x -> t}}, this allows for any number of additional arguments {{code|x}}.{{Explain|reason=Either this should be rephrased in a simpler way, or better explained. It is not immediately understandable, especially for readers not knowing Haskell, which is likely the vast majority of readers.|date=May 2018}}
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.{{citation needed|date=February 2018}} In the functional language [[Haskell]], variadic functions can be implemented by returning a value of a [[type class]] {{code|T}}; if instances of {{code|T}} are a final return value {{code|r}} and a function {{code|1=(T t) => x -> t}}, this allows for any number of additional arguments {{code|x}}.{{Explain|reason=Either this should be rephrased in a simpler way, or better explained. It is not immediately understandable, especially for readers not knowing Haskell, which is most readers, even for this article.|date=May 2018}}


A related subject in [[term rewriting]] research is called '''hedges''', or '''hedge variables'''.<ref>[https://arxiv.org/abs/1503.00336 CLP (H): Constraint Logic Programming for Hedges]</ref> 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).
A related subject in [[term rewriting]] research is called '''hedges''', or '''hedge variables'''.<ref>[https://arxiv.org/abs/1503.00336 CLP (H): Constraint Logic Programming for Hedges]</ref> 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).
Line 19: Line 20:


===In C===
===In C===
To portably implement variadic functions in the [[C (programming language)|C programming language]], the standard [[stdarg.h|{{code|stdarg.h}}]] header file is used. The older [[varargs.h|{{code|varargs.h}}]] header has been [[Deprecation|deprecated]] in favor of {{code|stdarg.h}}. In C++, the header file {{code|cstdarg}} is used.<ref>{{cite web|url=http://www.cplusplus.com/reference/clibrary/cstdarg/|title=<cstdarg> (stdarg.h) - C++ Reference|website=www.cplusplus.com}}</ref>
To portably implement variadic functions in the [[C (programming language)|C language]], the standard [[stdarg.h|{{code|stdarg.h}}]] header file is used. The older [[varargs.h|{{code|varargs.h}}]] header has been [[Deprecation|deprecated]] in favor of {{code|stdarg.h}}. In C++, the header file {{code|cstdarg}} is used.<ref>{{cite web|url=http://www.cplusplus.com/reference/clibrary/cstdarg/|title=<cstdarg> (stdarg.h) - C++ Reference|website=www.cplusplus.com}}</ref>


<syntaxhighlight lang="C">
<syntaxhighlight lang="C">
Line 47: Line 48:
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 {{code|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 {{code|NULL}} may be used to indicate the number.) 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]].
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 {{code|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 {{code|NULL}} may be used to indicate the number.) 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]].


{{code|stdarg.h}} declares a type, {{code|va_list}}, and defines four macros: [[va_start|{{code|va_start}}]], [[va_arg|{{code|va_arg}}]], [[va_copy|{{code|va_copy}}]], and [[va_end|{{code|va_end}}]]. Each invocation of {{code|va_start}} and {{code|va_copy}} must be matched by a corresponding invocation of {{code|va_end}}. When working with variable arguments, a function normally declares a variable of type {{code|va_list}} ({{code|ap}} in the example) that will be manipulated by the macros.
{{code|stdarg.h}} declares a type, {{code|va_list}}, and defines four macros: [[va start|{{code|va_start}}]], [[va arg|{{code|va_arg}}]], [[va copy|{{code|va_copy}}]], and [[va end|{{code|va_end}}]]. Each invocation of {{code|va_start}} and {{code|va_copy}} must be matched by a corresponding invocation of {{code|va_end}}. When working with variable arguments, a function normally declares a variable of type {{code|va_list}} ({{code|ap}} in the example) that will be manipulated by the macros.


# {{code|va_start}} takes two arguments, a {{code|va_list}} object and a reference to the function's last parameter (the one before the ellipsis; the macro uses this to get its bearings). In [[C2x|C23]], the second argument will no longer be required and variadic functions will no longer need a named parameter before the ellipsis.{{r|g=note|n=KandR|r=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.<ref>{{cite web|url=https://thephd.dev/c23-is-coming-here-is-what-is-on-the-menu#n2975---relax-requirements-for-variadic-parameter-lists|title=C23 is Finished: Here is What is on the Menu §N2975 - Relax requirements for variadic parameter lists|date=31 July 2022 }}</ref>}}<ref name=N2975>{{cite web|url=https://open-std.org/JTC1/SC22/WG14/www/docs/n2975.pdf|title=WG14-N2975 : Relax requirements for variadic parameter lists, v3|date=2022-04-15|last1=Gilding|first1=Alex|last2=Meneide|first2=JeanHeyd}}</ref> It initialises the {{code|va_list}} object for use by {{code|va_arg}} or {{code|va_copy}}. 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.
# {{code|va_start}} takes two arguments, a {{code|va_list}} 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 (C standard revision)|C23]], the second argument will no longer be required and variadic functions will no longer need a named parameter before the ellipsis.{{r|g=note|n=KandR|r=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.<ref>{{cite web|url=https://thephd.dev/c23-is-coming-here-is-what-is-on-the-menu#n2975---relax-requirements-for-variadic-parameter-lists|title=C23 is Finished: Here is What is on the Menu §N2975 - Relax requirements for variadic parameter lists|date=31 July 2022 }}</ref>}}<ref name=N2975>{{cite web|url=https://open-std.org/JTC1/SC22/WG14/www/docs/n2975.pdf|title=WG14-N2975 : Relax requirements for variadic parameter lists, v3|date=2022-04-15|last1=Gilding|first1=Alex|last2=Meneide|first2=JeanHeyd}}</ref> It initialises the {{code|va_list}} object for use by {{code|va_arg}} or {{code|va_copy}}. 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.
# {{code|va_arg}} takes two arguments, a {{code|va_list}} object (previously initialised) and a type descriptor. It expands to the next variable argument, and has the specified type. Successive invocations of {{code|va_arg}} allow processing each of the variable arguments in turn. Unspecified behavior occurs if the type is incorrect or there is no next variable argument.
# {{code|va_arg}} takes two arguments, a {{code|va_list}} object (previously initialised) and a type descriptor. It expands to the next variable argument, and has the specified type. Successive invocations of {{code|va_arg}} allow processing each of the variable arguments in turn. Unspecified behavior occurs if the type is incorrect or there is no next variable argument.
# {{code|va_end}} takes one argument, a {{code|va_list}} 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 {{code|va_list}} object by invoking {{code|va_end}} and then {{code|va_start}} again on it.
# {{code|va_end}} takes one argument, a {{code|va_list}} 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 {{code|va_list}} object by invoking {{code|va_end}} and then {{code|va_start}} again on it.
Line 82: Line 83:


===In C++===
===In C++===
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 <code>va_start</code> requires the name of the last fixed argument of the function. <!-- When C23 is released the text should be updated to reflect that this is a difference between the languages. When C++26 ports the one argument va_start to C++ (https://wg21.link/p2537) this should be updated again. See also the C section of the article. -->
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 parameter]]s but provides no way to access those arguments since <code>va_start</code> requires the name of the last fixed argument of the function. <!-- When C23 is released the text should be updated to reflect that this is a difference between the languages. When C++26 ports the one argument va_start to C++ (https://wg21.link/p2537) this should be updated again. See also the C section of the article. -->


<syntaxhighlight lang="c++">
<syntaxhighlight lang="c++">
Line 252: Line 253:
</syntaxhighlight>
</syntaxhighlight>


===In [[Pascal (programming language)|Pascal]]===
===In Pascal===
[[Pascal (programming language)|Pascal]] is standardized by [[International Organization for Standardization|ISO]] standards 7185 (“Standard Pascal”) and 10206 (“Extended Pascal”).
{{cleanup section|reason=Default and polymorphism is not variadic. I/O example is too long.|date=April 2021}}
Neither standardized form of Pascal supports variadic routines, ''except'' for certain [[Intrinsic function|built-in routines]] ({{code|read|pascal}}/{{code|readLn|pascal}} and {{code|write|pascal}}/{{code|writeLn|pascal}}, and additionally in {{abbr|EP|Extended Pascal}} {{code|readStr|pascal}}/{{code|writeStr|pascal}}).
[[Pascal (programming language)|Pascal]] has four built-in procedures which are defined as variadic, which, because of this special condition, are intrinsic to the compiler. These are the {{code|read}}, {{code|readln}}, {{code|write}}, and {{code|writeln}} procedures. However, there are alternate specifications allowing for [[default argument|''default'' arguments]] to procedures or functions which make them work variadically, as well as ''[[polymorphism (computer science)|polymorphism]]'' which allows a procedure or function to have different parameters.


Nonetheless, ''dialects'' of Pascal implement mechanisms ''resembling'' variadic routines.
The {{code|read[ln]}} and {{code|write[ln]}} procedures all have the same format:
[[Delphi (programming language)|Delphi]] defines an {{code|array of const|delphi}} data type that may be associated with the ''last'' [[formal parameter]].
Within the routine definition the {{code|array of const|delphi}} is an {{code|array of TVarRec|delphi}}, an [[Array (data type)|array]] of [[variant record]]s.<ref name="delphi">{{cite web|url=https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Parameters_(Delphi)#Variant_Open_Array_Parameters|title=Parameters (Delphi)|access-date=2023-08-28}}</ref>
The {{code|VType|delphi}} member of the aforementioned {{code|record|delphi}} data type allows inspection of the argument’s data type and subsequent appropriate handling.
The [[Free Pascal Compiler]] supports Delphi’s variadic routines, too.<ref name="fpc">{{cite web|url=https://www.freepascal.org/docs-html/3.2.0/ref/refsu68.html|title=Free Pascal - Reference guide|access-date=2023-08-28}}</ref>


This implementation, however, technically requires a ''single'' argument, that is an {{code|array|pascal}}.
<pre>
Pascal imposes the restriction that arrays need to be homogenous.
read[ln] [( [file ,] variable [, variable ...] )] ;
This requirement is circumvented by utilizing a variant record.
write[ln] [( [file][, value [, value ...] )] ;
The [[GNU Pascal]] defines a real variadic formal parameter specification using an ellipsis ({{code|...|pascal}}), but as of 2022 no portable mechanism to use such has been defined.<ref name="gpc">{{cite web|url=https://www.gnu-pascal.de/gpc/Special-Parameters.html|title=The GNU Pascal Manual|access-date=2023-08-28}}</ref>
</pre>

where

* {{code|file}} is an optional file variable, which if omitted, defaults to {{code|input}} for {{code|read}} and {{code|readln}}, or defaults to {{code|output}} for {{code|write}} and {{code|writeln}};
* {{code|variable}} is a scalar such as a char (character), integer, or real (or for some compilers, certain record types or array types such as strings); and
* {{code|value}} is a variable or a constant.

Example:

<syntaxhighlight lang="pascal" line>
var
f: text;
ch: char;
n,a,I,B: Integer;
S: String;

begin
Write('Enter name of file to write results: ');
readln(s);
assign(f,S);
rewrite(f);
Write('What is your name? ');
readln(Input,S);
Write('Hello, ',S,'! Enter the number of calculations you want to do:');
writeln(output);
Write('? ');
readln(N);
Write('For each of the ',n,' formulas, enter ');
write('two integers separated by one or more spaces');
writeln;
for i := 1 to N do
begin
Write('Enter pair #',i,'? ');
read(a,b);
READLN;
WRITELN(Out,'A [',a,'] + B [',B,'] =',A+B);
end;
close(OUT);
end.
</syntaxhighlight>

In the above example, as far as the compiler is concerned, lines 9 and 13 are identical, because if {{code|input}} is the file variable being read into by a {{code|read}} or {{code|readln}} statement, the file variable may be omitted. Also, the compiler considers lines 15 and 20 to be identical, because if the file variable being written to is {{code|output}}, it can be omitted, which means (on line 20) since there are no arguments being passed to the procedure the parentheses listing arguments can be omitted. Line 26 shows the {{code|writeln}} statement can have any number of arguments, and they can be a quoted string, a variable, or even a formula result.

Object Pascal supports ''polymorphic'' procedures and functions, where different procedure(s) or function(s) can have the same name but are distinguished by the arguments supplied to them.

Pascal also supports ''default'' arguments, where the value of an argument, if not provided, is given a default value.

For the first example, polymorphism, consider the following:

<syntaxhighlight lang="pascal" line>
function add(a1,a2:integer):Integer; begin add := a1+a2 end;
function add(r1,r2:real):real; begin add := a1+a2 end;
function add(a1:integer;r2:real):real; begin add := real(a1)+a2 end;
function add(r1:real,a2:integer):real; begin add := a1+real(a2) end;
</syntaxhighlight>

In the above example, if {{code|add}} as called with two integer values, the function declared on line 1 would be called; if one of the arguments is an integer and one is real, either the function on line 3 or 4 is called depending on which is integer. If both are real, the function on line 2 is called.

For default parameters, consider the following:

<syntaxhighlight lang="pascal" line>
const
Three = 3;
var
K: Integer;

function add(i1: integer = 0;
i2: integer = 0;
i3: integer = 0;
i4: integer = 0;
i5: integer = 0;
i6: integer = 0;
i7: integer = 0;
i8: integer = 0): integer;
begin
add := i1+i2+i3+I4+I5+i6+I7+I8;
end;

begin
K := add; { K is 0}
K := add(K,1); { K is 1}
K := add(1,2); { K is 3}
K := add(1,2,Three); { K is 6, etc.}
end.
</syntaxhighlight>


Both GNU Pascal and FreePascal allow externally declared functions to use a variadic formal parameter specification using an ellipsis ({{code|...|pascal}}).
On Line 6, (and the lines below) the parameter {{code|1== 0}} tells the compiler, "if no argument is provided, presume the argument to be zero". On line 19, no arguments were given, so the function returns {{code|0}}. On line 20, either a number or a variable can be supplied for any argument, and as shown on line 22, a constant.


===In [[PHP]]===
===In PHP===
[[PHP]] does not care about types of variadic arguments unless the argument is typed.
[[PHP]] does not care about types of variadic arguments unless the argument is typed.


<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
Line 405: Line 325:


====Unflattened slurpy====
====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:
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="perl6">
<syntaxhighlight lang="perl6">
Line 555: Line 475:


==External links==
==External links==
* [http://rosettacode.org/wiki/Variadic_function Variadic function]. [[Rosetta Code]] task showing the implementation of variadic functions in over fifty programming languages.
* [http://rosettacode.org/wiki/Variadic_function Variadic function]. [[Rosetta Code]] task showing the implementation of variadic functions in over 120 programming languages.
* [https://web.archive.org/web/20070927215504/http://www.codeproject.com/cpp/argfunctions.asp?df=100&forumid=15556&exp=0&select=503481 Variable Argument Functions] — A tutorial on Variable Argument Functions for C++
* [https://web.archive.org/web/20070927215504/http://www.codeproject.com/cpp/argfunctions.asp?df=100&forumid=15556&exp=0&select=503481 Variable Argument Functions] — A tutorial on Variable Argument Functions for C++
* [https://www.gnu.org/software/hello/manual/libc/Variadic-Functions.html GNU libc manual]
* [https://www.gnu.org/software/hello/manual/libc/Variadic-Functions.html GNU libc manual]


[[Category:Subroutines]]
[[Category:Subroutines]]
[[Category:Programming language comparisons]]
<!-- Hidden categories below -->
[[Category:Articles with example C code]]
[[Category:Articles with example C++ code]]
[[Category:Articles with example C Sharp code]]
[[Category:Articles with example Haskell code]]
[[Category:Articles with example Java code]]
[[Category:Articles with example JavaScript code]]
[[Category:Articles with example Pascal code]]
[[Category:Articles with example Perl code]]
[[Category:Articles with example Python (programming language) code]]
[[Category:Articles with example Python (programming language) code]]
[[Category:Articles with example Ruby code]]
[[Category:Articles with example Rust code]]
[[Category:Articles with example Swift code]]
[[Category:Articles with example Tcl code]]

Revision as of 15:47, 27 July 2024

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–1937.[1] 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 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.[citation needed] 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.[further explanation needed]

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).

Examples

In 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 stdarg.h. In C++, the header file cstdarg is used.[4]

#include <stdarg.h>
#include <stdio.h>

double average(int count, ...) {
    va_list ap;
    int j;
    double sum = 0;

    va_start(ap, count); /* Before C23: Requires the last fixed parameter (to get the address) */
    for (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 const *argv[]) {
    printf("%f\n", average(3, 1, 2, 3));
    return 0;
}

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 may be used to indicate the number.) 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.

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.

  1. va_start takes two arguments, a va_list 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.[note 1][6] It initialises the va_list object for use by va_arg or va_copy. 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.
  2. va_arg takes two arguments, a va_list object (previously initialised) and a type descriptor. It expands to the next variable argument, and has the specified type. Successive invocations of va_arg allow processing each of the variable arguments in turn. Unspecified behavior occurs if the type is incorrect or there is no next variable argument.
  3. va_end takes one argument, a va_list 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 va_list object by invoking va_end and then va_start again on it.
  4. va_copy takes two arguments, both of them va_list 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 va_start on a first va_list, then using va_copy to clone it into a second va_list. After scanning the variable arguments a first time with va_arg and the first va_list (disposing of it with va_end), the programmer could scan the variable arguments a second time with va_arg and the second va_list. va_end needs to also be called on the cloned va_list before the containing function returns.

In C#

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.

using System;

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 = new int[] { 13, 14, 15 };
        Console.WriteLine(Foo(1, 2, manyValues));  // 42
    }
}

In C++

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.

#include <iostream>
#include <cstdarg>

void simple_printf(const char* fmt...)      // C-style "const char* fmt, ..." is also valid
{
    va_list args;
    va_start(args, fmt);
 
    while (*fmt != '\0') {
        if (*fmt == 'd') {
            int i = va_arg(args, int);
            std::cout << i << '\n';
        } else if (*fmt == 'c') {
            // note automatic conversion to integral type
            int c = va_arg(args, int);
            std::cout << static_cast<char>(c) << '\n';
        } else if (*fmt == 'f') {
            double d = va_arg(args, double);
            std::cout << d << '\n';
        }
        ++fmt;
    }
 
    va_end(args);
}

int main()
{
    simple_printf("dcff", 3, 'a', 1.999, 42.5); 
}

Variadic templates (parameter pack) can also be used in C++ with language built-in fold expressions.

#include <iostream>

template <typename... Ts>
void foo_print(Ts... args) 
{
    ((std::cout << args << ' '), ...);
}

int main()
{
    std::cout << std::boolalpha;
    foo_print(1, 3.14f); // 1 3.14
    foo_print("Foo", 'b', true, nullptr); // Foo b true nullptr
}

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]

In Go

Variadic functions in Go can be called with any number of trailing arguments.[8] fmt.Println is a common variadic function; it uses an empty interface as a catch-all type.

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"
}

Output:

The sum of [1 2] is 3 
The sum of [1 2 3] is 6 
The sum of [1 2 3 4] is 10

In Java

As with C#, the Object type in Java is available as a catch-all.

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 string : strings) {
            System.out.println(string);
        }
    }

    public static void main(String[] args) {
        printArgs("hello");          // short for printArgs(["hello"])
        printArgs("hello", "world"); // short for printArgs(["hello", "world"])
    }
}

In JavaScript

JavaScript does not care about types of variadic arguments.

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

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.

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

In Lua

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 higher[9] table.unpack, or Lua 5.1 or lower[10] unpack. Varargs can be used as a table by constructing a table with the vararg as a value.

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

In 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 (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.[11] 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.[12]

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.[13]

Both GNU Pascal and FreePascal allow externally declared functions to use a variadic formal parameter specification using an ellipsis (...).

In PHP

PHP does not care about types of variadic arguments unless the argument is typed.

function sum(...$nums): int
{
    return array_sum($nums);
}

echo sum(1, 2, 3); // 6

And typed variadic arguments:

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)

In Python

Python does not care about types of variadic arguments.

def foo(a, b, *args):
    print(args)  # args is a tuple (immutable sequence).

foo(1, 2) # ()
foo(1, 2, 3) # (3,)
foo(1, 2, 3, "hello") # (3, "hello")

Keyword arguments can be stored in a dictionary, e.g. def bar(*args, **kwargs).

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 (i.e, Iterables).

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]

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:

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]]

Contextual slurpy

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).

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 **@

In Ruby

Ruby does not care about types of variadic arguments.

def foo(*args)
  print args
end

foo(1)
# prints `[1]=> nil`

foo(1, 2)
# prints `[1, 2]=> nil`

In Rust

Rust does not support variadic arguments in functions. Instead, it uses macros.[14]

macro_rules! calculate {
    // The pattern for a single `eval`
    (eval $e:expr) => {{
        {
            let val: usize = $e; // Force types to be integers
            println!("{} = {}", stringify!{$e}, val);
        }
    }};

    // Decompose multiple `eval`s recursively
    (eval $e:expr, $(eval $es:expr),+) => {{
        calculate! { eval $e }
        calculate! { $(eval $es),+ }
    }};
}

fn main() {
    calculate! { // Look ma! Variadic `calculate!`!
        eval 1 + 2,
        eval 3 + 4,
        eval (2 * 3) + 1
    }
}

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.[15]

In 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"])
  }
}

In Swift

Swift cares about the type of variadic arguments, but the catch-all Any type is available.

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

In Tcl

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.[16][17]

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

See also

Notes

  1. ^ 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]

References

  1. ^ 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.
  2. ^ Klemens, Ben (2014). 21st Century C: C Tips from the New School. O'Reilly Media, Inc. p. 224. ISBN 978-1491904442.
  3. ^ CLP (H): Constraint Logic Programming for Hedges
  4. ^ "<cstdarg> (stdarg.h) - C++ Reference". www.cplusplus.com.
  5. ^ "C23 is Finished: Here is What is on the Menu §N2975 - Relax requirements for variadic parameter lists". 31 July 2022.
  6. ^ Gilding, Alex; Meneide, JeanHeyd (2022-04-15). "WG14-N2975 : Relax requirements for variadic parameter lists, v3" (PDF).
  7. ^ "DCL50-CPP. Do not define a C-style variadic function".
  8. ^ "Go by Example: Variadic Functions".
  9. ^ "Lua 5.2 Reference Manual". www.lua.org. Retrieved 2023-02-05.
  10. ^ "Lua 5.1 Reference Manual". www.lua.org. Retrieved 2023-02-05.
  11. ^ "Parameters (Delphi)". Retrieved 2023-08-28.
  12. ^ "Free Pascal - Reference guide". Retrieved 2023-08-28.
  13. ^ "The GNU Pascal Manual". Retrieved 2023-08-28.
  14. ^ "Variadics". Rust By Example.
  15. ^ "2137-variadic". The Rust RFC Book.
  16. ^ "proc manual page". Tcl/Tk Documentation.
  17. ^ "args". Tcler's Wiki.