Variadic function: Difference between revisions

Content deleted Content added
m Link to variadic macro
+ var'length arg lists in PHP, +optional args with default values
Line 6:
 
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.
 
In PHP, [http://php.net/functions_arguments variable-length argument lists] are natively supported (without security risk) since version 4; dedicated functions (func_num_args, func_get_arg, func_get_args) allow the programmer to determine the number and values of unspecified arguments.
 
Besides the concept of a ''completely arbitrary number'' of arguments,
the declaration of '''[[default value]]s''' for ''some'' of the ''specified arguments'' is another concept allowing to call a function with variable number of arguments.
Several [[typed language]]s implement this, and can in some cases associate the arguments to the corresponding variables in view of their type, even if they are given at [[call-time]] in an order differing from the declaration.
In languages which are not (strongly) typed (such as PHP), such [[optional argument]]s with default values cannot precede [[mandatory]] arguments without default value. (An exception are some system functions of PHP, like [http://php.net/implode the implode function], that can associate arguments given in any order to the right variables, in view of their type.)
 
== See Also ==