Variadic function: Difference between revisions

Content deleted Content added
GNU Pascal and FreePascal allow externally declared functions to use an ellipsis
m WP:LINKs: add, update-standardize, needless WP:PIPE > WP:NOPIPE. Small WP:COPYEDITs WP:EoS: WP:TERSE, clarify. WP:CATEGORYs add.
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>
 
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 (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 likelymost thereaders, vasteven majorityfor ofthis readersarticle.|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).
Line 20:
 
===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>
 
<syntaxhighlight lang="C">
Line 476:
==External links==
* [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://www.gnu.org/software/hello/manual/libc/Variadic-Functions.html GNU libc manual]
 
[[Category:Subroutines]]
 
[[Category:Articles with example C 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]]