Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Variadic function
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==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 (programming language)|C]] function [[printf|{{code|printf}}]] and the [[Common Lisp]] function [[Format (Common Lisp)|{{code|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|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]], 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).
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)