VARIADIC FUNCTION
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 languages.
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 function printf and the Common Lisp function format 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 printf, if used uncautiously, can give rise to a class of security holes known as format string attacks. 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 than were placed there -- corrupting the stack and leading to unexpected behavior.
| Contents |
| Specific implementations |
| Variadic functions in C and C++ |
| Variadic functions in C# and Java |
| Variadic functions in JavaScript |
| Variadic functions in PHP |
| Variadic functions in Python |
| See also |
Specific implementations
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
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.
For a simple tutorial refer to the Variable Argument Functions tutorial.
template
void print( const T& t )
{
std::cout << t;
}
template< typename T, typename... Args>
void print( const T& t, const Args&... args )
{
print( t );
print( args... );
}
template
class tuple;
template
class tuple
template<>
class tuple<>;
Variadic functions in C# and Java
Other languages, such as C# and 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.
This feature, called "varargs", has been present in Java since J2SE 5.0. Using autoboxing, this can also be used to pass primitive arguments to for example a print function, as can be seen in the Java example below:
public static void printSpaced(Object... objects) {
for (Object o : objects)
System.out.print(o + " ");
}
// Can be used to print:
// printSpaced(1, 2, "three");
C# Example:
public static void PrintSpaced(params Object[] objects)
{
foreach (Object o in objects) //objects is an array. We can use foreach loop to iterate between the objects.
Console.WriteLine(o + " ");
}
// Can be used to print:
// 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' [1].
function printSpaced() {
for (var i = 0; i < arguments.length; i++) {
document.write(arguments[i] + " ");
}
}
Variadic functions in PHP
In PHP, 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.
Variadic functions in Python
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 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. BeautifulSoup). If they exist, these arguments must be the last one in the list.
def f(
★ args,
★
★ kwargs):
print args
print kwargs
f(1,2,"cow","kitty")
> (1, 2, "cow", "kitty")
> {}
f(arg1=1,sample=2,name="cow",hero="kitty")
> ()
> {"arg1":1, "sample":2, "name":"cow", "hero":"kitty"}
f(1,2,name="cow",hero="kitty")
> (1, 2)
> {"name":"cow", "hero":"kitty"}
f(arg1=1,sample=2,name="cow", "kitty") -> SyntaxError "Non-keyword arg after keyword arg"
Conversely you may also pass in a tuple or dictionary using the same asterisk-notation and have it automatically expand to fill.
def g(a, b, c):
print a, b, c
mytuple = 1,2,3
mydict = {"a":"first", "b":"second", "c":"third"}
g(
★ mytuple) -> 1 2 3
g(
★
★ mydict) -> first second third
g(
★
★ {"a":"first"}) -> TypeError "g() takes exactly 3 non-keyword arguments (got 1)"
g(
★
★ {"a":"first", "b":"second", "c":"third", "d":"fourth"}) -> TypeError "g() got an unexpected keyword argument 'd'"
See also
★ Variadic macro (C programming language)
★ Java syntax
★ A very simple tutorial on Variable Argument Functions for C++ Variable Argument Functions
This article provided by Wikipedia. To edit the contents of this article, click here for original source.
psst.. try this: add to faves

العربية
中国
Français
Deutsch
Ελληνική
हिन्दी
Italiano
日本語
Português
Русский
Español