Variadic function: Difference between revisions

Content deleted Content added
Dcoetzee (talk | contribs)
source of errors
expanding -- C printf and Lisp format as practical examples; type safety
Line 1:
In [[computer programming]], a '''variadic function''' is a [[function]] of variable [[arity]]; that is, one which can take different numbers of arguments. Support for variadic functions differs widely among [[programming language]]s. They are also an unfortunate source of errors because they are usually not [[strongly-typed]].
 
There are many mathematical and logical operations which come across naturally as variadic functions. For instance, the summing of numbers or the concatenation of strings or other sequences are operations that can logically apply to any number of operands.
 
Another operation which has been implemented as a variadic function in many languages is output formatting. The [[C programming language|C]] function <TT>[[printf]]</TT> and the [[Common Lisp]] function <TT>format</TT> are two such examples. Both take one argument which specifies the formatting of the output, and ''any number'' of arguments which provide the values to be formatted.
 
Variadic functions can expose type-safety problems in some languages. For instance, C's <TT>printf</TT>, if used uncautiously, 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 safety|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.
 
{{compu-stub}}