Variadic function: Difference between revisions

Content deleted Content added
Variadic functions in C# and Java: not all versions of java
No edit summary
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.
 
== Specific implementations ==
== Variadic functions in C and C++ ==
The following provides an overview of specific implementations in different programming languages and environments.
 
=== Variadic functions in 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 <tt><stdarg.h></tt>.
 
Line 37 ⟶ 39:
class tuple<>;
 
=== Variadic functions in PHPC# and Java ===
 
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# and Java ==
 
Other languages, such as [[C Sharp|C#]] and [[Java (programming language)|Java]] use a different approach&mdash;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]].
 
Line 66 ⟶ 63:
// printSpaced(1, 2, "three");
 
=== Variadic functions in JavaScript ===
In [[JavaScript]], the arguments to a function may be accessed as local variables within the function.
They also exist as members of a local array within the function, called '''arguments''' [http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Functions:arguments].
Line 76 ⟶ 73:
}
 
=== Variadic functions in PythonPHP ===
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# and JavaPython ===
{{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.
 
Line 113 ⟶ 112:
 
==See also==
 
* [[Variadic macro]] (C programming language)
* [[Java syntax]]