Variadic function

As a variadic function is called in programming functions, procedures, or methods with indefinite arity, ie those whose number of parameters is not already specified in its declaration. In some languages, such as C, C and Java, this is displayed in the function declaration with an ellipsis, the so-called ellipse. In place of the ellipse as many arguments (or none) can be passed. Useful are variadic functions, for example, in the linking of several strings or the summing of number sequences and generally in operations that can be applied in principle to any number of operands.

Implementation in various programming languages

C and C

Access to variadic parameters is carried out via special macros. Some library functions of C are declared with ellipsis, since there is no possibility of function overloading in C, examples are the library functions printf and scanf for formatted off or input. A fundamental problem with this mechanism is that at runtime is no information on the number and type of arguments available. Thus, the programmer must ensure by additional measures (such as the format string in printf ) ensure that the arguments are interpreted correctly and no more arguments are processed than were actually present. In addition, at least you must specify a fixed parameter before the ellipsis.

In C ellipses are now considered obsolete, because C provides more elegant ways to function overloading and the concept of default arguments, and considered bad because they do not provide type safety. However, they are the only way to a virtually unlimited number of arguments to pass.

C #

In C #, the keyword " params " used in the declaration. Also, the type is mitangegeben. One such method may be a comma-separated list passed (this list may be empty). According to the " params " keyword no other parameters are more acceptable. In addition, the " params " keyword may appear only once in the Methodendeklarartion.

Public class sum {      public static int sum ( params int [ ] list )      {          int result = 0;            for (int i = 0; i < List.length; i )          {              result = list [i ];          }            return result;      }        static void Main ()      {          int a = sum (); / / Result: a = 0          int b = Sum ( 1); / / Result: b = 1          int c = sum ( 1, 2, 3, 4); / / Result: c = 10      } } Go

The Go language allows you to declare variadic functions via the attachment of an ellipse ... the name of the last parameter. It can then pass any number of parameters of the specified type, these are then available on a slice in the called function. It is also possible by adding an ellipse to the last parameter in the call to a variadic function to directly pass a slice instead of individual parameters. Here is an example similar to the above C # example.

/ / Declare a function Sum, the int called with any number of parameters of the type / / Is and returns the sum of its parameters. func Sum ( int x ... ) ( int n ) {      for i: = range x {/ / Iterate over all the indices of x          n = x [ i]      }        return }   / / Example function to call the Sum () function ExampleSum func () {      Sum ( ) / / empty sum. Returns 0      Sum ( 1, 2, 3) / / returns 6. x has the value [ ] has int {1, 2, 3}      v: = [] int {23, 42, 1337 }      Sum ( v. ..) / / How Sum ( 23, 42, 1337) } Java

Also in Java is used in the declaration of the ellipse. These are known as methods with variable number of arguments, or varargs shortly. Unlike C and C but the type is specified with. In the background, the parameter list is translated into an array, so that as an array must also be treated within the function body of the parameters.

In JavaScript, the arbitrary number of function arguments are passed to the arguments object, which arguments.length an array - like object with additional properties and is arguments.callee. Using the arguments object even the function parameters can be overwritten, as the following example shows.

My radio function (x ) {      arguments = 5; / / The variable x is overwritten here!      ... / / X is now 5 } PHP

PHP only supports variadic version 4 functions that earlier versions of PHP were not of such a mechanism. There is no particular characteristic used in the function declaration. The arguments can be evaluated with the help of special functions.

Ruby

In Ruby variable argument numbers are indicated by an asterisk in front of the parameter names. Again, this parameter is treated as an array, where all arguments are collected, following the specified number of arguments.

Python

Haskell

Haskell does not allow right to use variable argument numbers, but you can replicate this on almost any clever use of type classes. ( This is also type-safe variadic functions with different argument types are possible with an appropriate implementation for example ).

256820
de