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:
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 [http://www.cplusplus.com/reference/clibrary/cstdarg/ 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"
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