Variadic function

In mathematics and in computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages.

Overview

There are many mathematical and logical operations that 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 that 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 that specifies the formatting of the output, and any number of arguments that provide the values to be formatted.

Variadic functions can expose type-safety problems in some languages. For instance, C's printf, if used incautiously, 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. As a consequence of this, the CERT Coordination Center considers variadic functions in C to be a high-severity security risk.[1]

In functional languages variadics can be considered complementary to the apply function, which takes a function and a list/sequence/array as arguments, and calls the function with the arguments supplied in that list, thus passing a variable number of arguments to the function. In the functional language Haskell, variadic functions can be implemented by returning a value of a type class T; if instances of T are a final return value r and a function (T t) => x -> t, this allows for any number of additional arguments x.

A related subject in term rewriting research is called hedges, or hedge variables.[2] Unlike variadics, which are functions with arguments, hedges are sequences of arguments themselves. They also can have constraints ('take no more than 4 arguments', for example) to the point where they are not variable-length (such as 'take exactly 4 arguments') - thus calling them variadics can be misleading. However they are referring to the same phenomenon, and sometimes the phrasing is mixed, resulting in names such as variadic variable (synonymous to hedge). Note the double meaning of the word variable and the difference between arguments and variables in functional programming and term rewriting. For example, a term (function) can have three variables, one of them a hedge, thus allowing the term to take three or more arguments (or two or more if the hedge is allowed to be empty).

Example in C

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

#include <stdarg.h>
#include <stdio.h>

double average(int count, ...)
{
    va_list ap;
    int j;
    double sum = 0;

    va_start(ap, count); /* Requires the last fixed parameter (to get the address) */
    for (j = 0; j < count; j++) {
        sum += va_arg(ap, int); /* Increments ap to the next argument. */
    }
    va_end(ap);

    return sum / count;
}

int main(int argc, char const *argv[])
{
	printf("%f\n", average(3, 1, 2, 3) );
	return 0;
}

This will compute the average of an arbitrary number of arguments. Note that the function does not know the number of arguments or their types. The above function expects that the types will be int, and that the number of arguments is passed in the first argument (this is a frequent usage but by no means enforced by the language or compiler). In some other cases, for example printf, the number and types of arguments are figured out from a format string. In both cases, this depends on the programmer to supply the correct information. If fewer arguments are passed in than the function believes, or the types of arguments are incorrect, this could cause it to read into invalid areas of memory and can lead to vulnerabilities like the format string attack.

stdarg.h declares a type, va_list, and defines four macros: va_start, va_arg, va_copy, and va_end. Each invocation of va_start and va_copy must be matched by a corresponding invocation of va_end. When working with variable arguments, a function normally declares a variable of type va_list (ap in the example) that will be manipulated by the macros.

  1. va_start takes two arguments, a va_list object and a reference to the function's last parameter (the one before the ellipsis; the macro uses this to get its bearings). It initialises the va_list object for use by va_arg or va_copy. The compiler will normally issue a warning if the reference is incorrect (e.g. a reference to a different parameter than the last one, or a reference to a wholly different object), but will not prevent compilation from completing normally.
  2. va_arg takes two arguments, a va_list object (previously initialised) and a type descriptor. It expands to the next variable argument, and has the specified type. Successive invocations of va_arg allow processing each of the variable arguments in turn. Unspecified behavior occurs if the type is incorrect or there is no next variable argument.
  3. va_end takes one argument, a va_list object. It serves to clean up. If you wanted to, for instance, scan the variable arguments more than once, you would re-initialise your va_list object by invoking va_end and then va_start again on it.
  4. va_copy takes two arguments, both of them va_list objects. It clones the second (which must have been initialised) into the first. Going back to the "scan the variable arguments more than once" example, this could be achieved by invoking va_start on a first va_list, then using va_copy to clone it into a second va_list. After scanning the variable arguments a first time with va_arg and the first va_list (disposing of it with va_end), you could scan the variable arguments a second time with va_arg and the second va_list. Don't forget to va_end the clone va_list.

Example in Go

Variadic functions [4] can be called with any number of trailing arguments. For example, fmt.Println is a common variadic function.

package main

import "fmt"

// This variadic function takes an arbitrary number of ints as arguments.
func sum(nums ...int) {
	fmt.Print("The sum of ", nums) // Also a variadic function.
	total := 0
	for _, num := range nums {
		total += num
	}
	fmt.Println(" is", total) // Also a variadic function.
}

func main() {
	// Variadic functions can be called in the usual way with individual
	// arguments.
	sum(1, 2)  // "The sum of [1 2] is 3"
	sum(1, 2, 3) // "The sum of [1 2 3] is 6"

	// If you already have multiple args in a slice, apply them to a variadic
	// function using func(slice...) like this.
	nums := []int{1, 2, 3, 4}
	sum(nums...) // "The sum of [1 2 3 4] is 10"
}

Output:

The sum of [1 2] is 3 
The sum of [1 2 3] is 6 
The sum of [1 2 3 4] is 10

Example in C#

using System;

class Program
{
    static int Foo(int a, int b, params int[] args)
    {
        // Return the sum of the integers in args, ignoring a and b.
        int sum = 0;
        foreach (int i in args)
            sum += i;
        return sum;
    }
        
    static void Main(string[] args)
    {
        Console.WriteLine(Foo(1, 2));  // 0
        Console.WriteLine(Foo(1, 2, 3, 10, 20));  // 33
    }
}

Example in PHP

function sum(...$nums)
{
    return array_sum($nums);
}

echo sum(1, 2, 3); // 6

Example in Java

public class Program {
    private static void printArgs(String... strings) {
        for (int i = 0; i < strings.length; i++) {
            String string = strings[i];
            System.out.printf("Argument %d: %s%n", i, string);
        }
    }

    public static void main(String[] args) {
        printArgs("hello", "world");
    }
}

Example in Python

def foo(a, b, *args):
    print(args)  # args is a tuple (unmutable list).

foo(1, 2) # ()
foo(1, 2, 3) # (3)
foo(1, 2, 3, "hello") # (3, "hello")

Example in Perl 6

sub foo($a, $b, *@args) {
    say @args.perl;   # @args is an Array with flattened arguments
}
sub bar($a, $b, **@args) {
    say @args.perl;   # @args is an Array with non-flattened arguments
}

foo(1, 2)                # []
foo(1, 2, 3)             # [3]
foo(1, 2, 3, "hello")    # [3 "hello"]
foo(1, 2, 3, [4, 5, 6])  # [3 4 5 6]
bar(1, 2, 3, [4, 5, 6])  # [3 [4 5 6]]

Example in Swift

func greet(timeOfTheDay: String, names: String...) {
    // here, names is [String]
    
    print("Looks like we have \(names.count) people")
    
    for name in names {
        print("Hello \(name), good \(timeOfTheDay)")
    }
}

greet(timeOfTheDay: "morning", names: "Joseph", "Clara", "William", "Maria")

// Output:
// Looks like we have 4 people
// Hello Joseph, good morning
// Hello Clara, good morning
// Hello William, good morning
// Hello Maria, good morning

Example in Javascript

function sum(...numbers) {
    var sumArgs = 0;
    numbers.forEach((num) => { sumArgs += num });
    return sumArgs
}

sum(1, 2, 3) // 6
sum(3, 2, 1) // 6

Example in C++

#include <iostream>
#include <string>

template<typename T>
T adder(T v) {
  return v;
}

template<typename T, typename... Args>
T adder(T first, Args... args) {
  return first + adder(args...);
}

int main(){
  long sum = adder(1, 2, 3, 8, 7);
  std::cout << sum << std::endl; // 21

  std::string s1 = "x", s2 = "aa", s3 = "bb", s4 = "yy";
  std::string ssum = adder(s1, s2, s3, s4);

  std::cout << ssum << std::endl; //xaabbyy
}

See also

References

  1. Klemens, Ben (2014). 21st Century C: C Tips from the New School. O'Reilly Media, Inc. p. 224. ISBN 1491904445.
  2. CLP (H): Constraint Logic Programming for Hedges
  3. "<cstdarg> (stdarg.h) - C++ Reference". www.cplusplus.com.
  4. https://gobyexample.com/variadic-functions
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.