Variadic function

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.

Variadic functionality can be considered complementary to the apply function, which takes a list or array of arguments and applies it to the function as its arguments.

Specific implementations

The following provides an overview of specific implementations in different programming languages and environments.

Variadic functions in Lisp

Lisp uses the &rest argument specifier to implement variadic functions. At execution time, the &rest variable collects all following arguments into one list. For example:

(defun average (&rest args) (when args (/ (apply #'+ args) (length args))))

Note that this uses the standard lisp add function which itself is a variadic function.

Variadic functions in C, Objective-C and C++

To portably implement variadic functions in the C programming language, the standard stdarg.h header file should be used. The older varargs.h header has been deprecated in favor of stdarg.h. In C++, the header file [ cstdarg] should be used.

To create a variadic function, an ellipsis (...) must be placed at the end of a parameter list. Inside the body of the function, a variable of type va_list must be defined. Then the macros va_start("va_list", "last fixed param"), va_arg("va_list", "cast type"), va_end("va_list") can be used. For example"


double average(int count, ...){va_list ap;int j;double tot;va_start(ap, count); //Requires the last fixed parameter (to get the address)for(j=0; j

This will compute the average of an arbitrary number of arguments. The number of arguments (not including the first) must be given to avoid dereferencing an invalid pointer (va_list is a void pointer type).

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. Variadic templates will finally allow the creation of true tuple classes in C++.

templatevoid print( const T& t ){ std::cout << t;}

template< typename T, typename... Args>void print( const T& t, const Args&... args ){ print( t ); print( args... );}

templateclass tuple;templateclass tuple;template<>class tuple<>;

Variadic functions in C# and Java

Other languages, such as C# and 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.

This feature, called "varargs", has been introduced to Java in 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) Console.Write(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 [] .

function printSpaced() { for (var i = 0; i < arguments.length; i++) { document.write(arguments [i] + " "); } }

Variadic functions in Perl

In Perl 5, functions don't have an explicit list of parameters (except optional "prototypes" that are used for limited type checking). All arguments are stored in an array called @_, which can be accessed in the function. Therefore no special effort is needed to accept variable number of arguments.

sub print_spaced { my @objects = @_; print "@objects ";}

sub print_names { my ($title, @names) = @_; print "This is the list of $title names: " foreach (@names) { print "Name: $_ ";

print_spaced(1, 2, "three");

Variadic functions in PHP

In PHP, [ variable-length argument lists] are natively supported (without security risks) 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'"

Variadic functions in S-Lang

S-Lang supports two mechanisms for passing optional arguments to afunction. The two mechanisms are illustrated by the readasciifunction that is distributed as part of the S-Lang library. Thefunction is used to read one or more data columns from an ascii (asopposed to binary) file:

readascii ("xy.dat", &x, &y);readascii ("xydy.dat", &x, &y, &dy);readascii ("score.dat", &name, &score, &date, &flags; format="%s %lf %s %d", skip=5);

The optional keyword/value pairs that follow the semi-colon in theargument list are called "qualifiers" in S-Lang parlance.

When a function is called, the "_NARGS" variable contains the numberof arguments passed to the function. This value does not include thenumber of qualifiers. Here is a simple example that performs the sumof the arguments (assumed here to be numeric) passed to a function:

define sum_values (){ if (_NARGS = 0) usage ("result = sum_values (val1, val2, ...);"); variable args = __pop_args (_NARGS); variable total = 0, arg; foreach arg (args) total += arg.value; return total;}vmessage ("Sum = %g", sum_values (1,2,3,4,5));

Here is a variant of the above that uses a qualifier to alternatebetween the sum and difference of the successive arguments. Thisexample makes use of the built-in sum function to add the elements ofan array.

define sum_values (){ if (_NARGS = 0) usage ("result = sum_values (val1, val2, ... ; alternate);"); variable args = __pop_args (_NARGS);

% Create an array from the arguments variable array = [__push_args(args)] ;

% If the sum is to alternate, multiply the odd elements by -1 if (qualifier_exists ("alternate")) array *= -1;

return sum(array);}vmessage ("Sum = %g", sum_values (1,2,3,4,5 ; alternate));

Variadic functions in Ruby

Ruby supports variadic functions natively. An asterisk before the last parameter in the function declaration declares that parameter to be an array into which all remaining input arguments are placed.

def printNames(listTitle, *nameList) puts "This is the list of #{listTitle} names:" nameList.each do |name
puts "Name: #{name}" endend

printNames("employee", "John", "Bob", "Chris")

#This code produces the following output:
#This is the list of employee names:
#Name: John
#Name: Bob
#Name: Chris

Variadic functions in Scheme

In Scheme, there is a dotted notation for representing "improper lists" (chains of pairs whose last cdr is not null). You can use this notation in argument lists to specify an argument to "dump" the rest of the arguments into.

If a function is defined with the "define" syntax, then you put a dot before the varargs argument

(define (print-spaced . args) (for-each (lambda (x) (display x) (display " ")) args))

(define (print-names title . names) (display "This is the list of ") (display title) (display " names:") (newline) (for-each (lambda (x) (display "Name: ") (display x) (newline)) names))

(print-spaced 1 2 "three")

If a function is defined with the "lambda" syntax, then it is the same thing, except that if there are no arguments before the varargs argument (i.e. if you want to put all the arguments into one thing), then you remove the parentheses altogether; as if to say, let this be the entire argument list.

(define print-spaced (lambda args (for-each (lambda (x) (display x) (display " ")) args)))

(define print-names (lambda (title . names) (display "This is the list of ") (display title) (display " names:") (newline) (for-each (lambda (x) (display "Name: ") (display x) (newline)) names)))

ee also

* Variadic macro (C programming language)
* Java syntax
* A very simple tutorial on Variable Argument Functions for C++ [ Variable Argument Functions]

Wikimedia Foundation. 2010.

Look at other dictionaries:

  • Variadic macro — A variadic macro is a feature of the C preprocessor whereby a macro may be declared to accept a varying number of arguments.Variable argument macros were introduced in the ISO/IEC 9899:1999 (C99) revision of the C Programming Language standard in …   Wikipedia

  • Variadic — In computer science, a variadic operator or function is one that can take a varying number of arguments; that is, its arity is not fixed.For specific articles, see: * Variadic function * Variadic macro in the C preprocessor * Variadic templates… …   Wikipedia

  • Map (higher-order function) — In many programming languages, map is the name of a higher order function that applies a given function to each element of a list, returning a list of results. They are examples of both catamorphisms and anamorphisms. This is often called apply… …   Wikipedia

  • C++0x — is the planned new standard for the C++ programming language. It is intended to replace the existing C++ standard, ISO/IEC 14882, which was published in 1998 and updated in 2003. These predecessors are informally known as C++98 and C++03. The new …   Wikipedia

  • Stdarg.h — is a header in the C standard library of the C programming language that allows functions to accept an indefinite number of arguments. C++ provides this functionality in the header ; the C header, though permitted, is deprecated in C++.The… …   Wikipedia

  • Apply — In mathematics and computer science, Apply is a function that applies functions to arguments. It is a central concept in programming languages derived from lambda calculus, such as LISP and Scheme, and also in functional languages. In particular …   Wikipedia

  • OpenHMPP — HMPP for Hybrid Multicore Parallel Programming. Based on a set of directives, OpenHMPP Standard is a programming model designed to handle hardware accelerators without the complexity associated with GPU programming. This approach based on… …   Wikipedia

  • SIGILL — Infobox Computing signal description = Illegal instruction action = Abnormal termination of the process ILL ILLOPC | illegal opcode ILL ILLOPN | illegal operand ILL ADR | illegal addressing mode ILL ILLTRP | illegal trap ILL PRVOPC | privileged… …   Wikipedia

  • Perl 6 — Infobox programming language name = Perl paradigm = Multi paradigm year = 2000 designer = Larry Wall latest release version = pre release latest release date = typing = dynamic, static influenced by = Perl 5, Haskell, Smalltalk influenced =… …   Wikipedia

  • C-- — Infobox programming language name = C paradigm = imperative year = 1997 typing = static, weak designer = Simon Peyton Jones and Norman Ramsey influenced by = CC is a C like programming language. Its creators, functional programming researchers… …   Wikipedia

We are using cookies for the best presentation of our site. Continuing to use this site, you agree with this.