Content deleted Content added
Hopefully fixed the last typo in the note Tags: Mobile edit Mobile web edit Advanced mobile edit |
Rescuing 1 sources and tagging 0 as dead.) #IABot (v2.0.9.5 |
||
(40 intermediate revisions by 27 users not shown) | |||
Line 1:
{{Short description|Function with variable number of arguments}}
{{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.
The term ''variadic'' is a [[neologism]], dating back to
==Overview==
Line 12 ⟶ 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>
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 [[
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 ⟶ 20:
===In C===
To portably implement variadic functions in the [[C (programming language)|C
<syntaxhighlight lang="C">
Line 45 ⟶ 46:
</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 {{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}} or {{code|nullptr}} may be used to indicate the
{{code|stdarg.h}} declares a type, {{code|va_list}}, and defines four 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 [[
# {{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_copy}} takes two arguments, both of them {{code|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 {{code|va_start}} on a first {{code|va_list}}, then using {{code|va_copy}} to clone it into a second {{code|va_list}}. After scanning the variable arguments a first time with {{code|va_arg}} and the first {{code|va_list}} (disposing of it with {{code|va_end}}), the programmer could scan the variable arguments a second time with {{code|va_arg}} and the second {{code|va_list}}.
===In C#===
Line 82 ⟶ 83:
===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
<syntaxhighlight lang="c++">
Line 137 ⟶ 138:
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.<ref>{{cite web |title=DCL50-CPP. Do not define a C-style variadic function |url=https://wiki.sei.cmu.edu/confluence/display/cplusplus/DCL50-CPP}}</ref>
=== In Fortran ===
Since the Fortran 90 revision, [[Fortran]] functions or subroutines can accept optional arguments:<ref>{{Cite web |title=Optional Arguments |url=https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-0/optional-arguments.html |access-date=2025-03-18 |website=Intel |language=en}}</ref> the argument list is still fixed, but the ones that have the {{code|optional}} attribute can be omitted in the function/subroutine call. The intrinsic function {{code|present()}} 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>
'''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>
===In Go===
Line 223 ⟶ 280:
</syntaxhighlight>
=== In [[
[[Lua (programming language)|Lua]] functions may pass varargs to other functions the same way as other values using the {{Code|return}} keyword. tables can be passed into variadic functions by using, in Lua version 5.2 or higher<ref>{{Cite web |title=Lua 5.2 Reference Manual |url=https://www.lua.org/manual/5.2/manual.html#pdf-table.unpack |access-date=2023-02-05 |website=www.lua.org}}</ref> {{Code|table.unpack}}, or Lua 5.1 or lower<ref>{{Cite web |title=Lua 5.1 Reference Manual |url=https://www.lua.org/manual/5.1/manual.html#pdf-unpack |access-date=2023-02-05 |website=www.lua.org}}</ref> {{Code|unpack}}. 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>
===In Pascal===
[[Pascal (programming language)|Pascal]] is standardized by [[International Organization for Standardization|ISO]] standards 7185 (“Standard Pascal”) and 10206 (“Extended Pascal”).
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}}).
Nonetheless, ''dialects'' of Pascal implement mechanisms ''resembling'' variadic routines.
[[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}}.
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 ({{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>
Both GNU Pascal and FreePascal allow externally declared functions to use a variadic formal parameter specification using an ellipsis ({{code|...|pascal}}).
===In
[[PHP]] does not care about types of variadic arguments unless the argument is typed.
<syntaxhighlight lang="php">
Line 376 ⟶ 381:
====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="perl6">
Line 400 ⟶ 405:
zaz(1, 2, 3); # [3]
zaz(1, 2, 3, "hello"); # [3 "hello"]
zaz(1, 2, [4, 5]); # [4, 5], single
zaz(1, 2, 3, [4, 5]); # [3, [4, 5]], behaving as **@
zaz(1, 2, 3, [4, 5], [6]); # [3, [4, 5], [6]], behaving as **@
Line 526 ⟶ 531:
==External links==
* [http://rosettacode.org/wiki/Variadic_function Variadic function]. [[Rosetta Code]] task showing the implementation of variadic functions in over
* [https://web.archive.org/web/20070927215504/http://www.codeproject.com/cpp/argfunctions.asp?df=100&forumid=15556&exp=0&select=503481 Variable Argument Functions] —
* [https://www.gnu.org/software/hello/manual/libc/Variadic-Functions.html GNU libc manual]
[[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 Ruby code]]
[[Category:Articles with example Rust code]]
[[Category:Articles with example Swift code]]
[[Category:Articles with example Tcl code]]
|