Variadic function: Difference between revisions

Content deleted Content added
m dab
m sp, date & link fixes; unlinking common words using AWB
Line 1:
{{Redirect|varargs|the varargs.h library in [[C (programming language)|C]]|varargs.h}}
 
In [[computer programming]], a '''variadic function''' is a [[function (programming)|function]] of variable [[arity]]; that is, one which can take different numbers of arguments. Support for variadic functions differs widely among [[programming language]]s.
Line 9:
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.
 
== Variadic functions in [[C (programming language)|C]] and [[C++]] ==
 
To portably implement variadic functions in the C programming language, the standard [http://www.opengroup.org/onlinepubs/009695399/basedefs/stdarg.h.html <tt><stdarg.h></tt>] header file should be used. The older [http://www.opengroup.org/onlinepubs/007908799/xsh/varargs.h.html <tt><varargs.h></tt>] header has been deprecated in favor of [http://www.opengroup.org/onlinepubs/009695399/basedefs/stdarg.h.html <tt><stdarg.h></tt>].
 
The variadic function feature is going to be present in the upcoming ''[[C++'']] language standard, [[C++0x]]; this feature is called "Variadic templates". This allows the creation of variadic template classes and variadic template functions:
 
template<typename T>
Line 35:
class tuple<>;
 
== Variadic functions in [[PHP]] ==
 
In ''[[PHP'']], [http://php.net/functions_arguments variable-length argument lists] are natively supported (without security risk) since version 4; dedicated functions (<TT>func_num_args</TT>, <TT>func_get_arg</TT>, <TT>func_get_args</TT>) allow the programmer to determine the number and values of unspecified arguments.
 
== Variadic functions in [[C Sharp|C#]] and [[Java (programming language)|Java]] ==
 
Other languages, such as ''[[C Sharp|C#'']] and ''[[Java (programming language)|Java'']] use a different approach - they just allow that a variable number of arguments of the same (super)type may be passed to a variadic function. Inside the method they are simply collected in an [[array]]. Using [[autoboxing]], this can also be used to pass [[Primitive type|primitive]] arguments to for example a print function, as can be seen in the Java example below:
 
public static void printSpaced(Object... objects) {
Line 51:
// printSpaced(1, 2, "three");
 
== Variadic functions in [[Python (programming language)|Python]] ==
 
== Variadic functions in [[Python (programming language)|Python]] ==
{{section-stub}}
 
[[Python (programming language)|Python]] supports very flexible variadic functions. By marking variables with one asterisk (e.g. *var) the given variable is defined to be a tuple of all the extra arguments. By marking variables with two asterisks (e.g. **var) the given variable is a dictionary of all extra [[Parameter (computer science) | keyword arguments]]; the keys are strings, which are the names that were . Conventionally these are called "args" and "kwargs" respectively, but they may be something else, and packages often make good use of this ability to improve readability (e.g. [[http://www.crummy.com/software/BeautifulSoup/documentation.html BeautifulSoup]]). If they exist, these arguments must be the last one in the list.
 
def f(*args, **kwargs):
Line 90 ⟶ 89:
==See also==
 
* [[Variadic macro]] (C Programmingprogramming Languagelanguage)
* [[Java syntax]]
 
[[Category:Programming constructs]]
[[Category:Programming language topics]]