Variadic function: Difference between revisions

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 clean up, removed: ?seq=1#metadata_info_tab_contents
Line 3:
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.
 
==Overview==
Line 47:
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_startva start|{{code|va_start}}]], [[va_argva arg|{{code|va_arg}}]], [[va_copyva copy|{{code|va_copy}}]], and [[va_endva 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.
Line 307:
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:
Line 326:
Three = 3;
var
K: Integer;
 
function add(i1: integer = 0;
Line 351:
 
===In [[PHP]]===
[[PHP]] does not care about types of variadic arguments unless the argument is typed.
 
<syntaxhighlight lang="php">