Jump to content

Variadic function

From Wikipedia, the free encyclopedia
(Redirected fromVariadic functions)

Inmathematicsand incomputer programming,avariadic functionis afunctionof indefinitearity,i.e., one which accepts a variable number ofarguments.Support for variadic functions differs widely amongprogramming languages.

The termvariadicis aneologism,dating back to 1936–1937.[1]The term was not widely used until the 1970s.

Overview

[edit]

There are many mathematical and logical operations that come across naturally as variadic functions. For instance, the summing of numbers or theconcatenationof strings or other sequences are operations that can be thought of as applicable to any number of operands (even though formally in these cases theassociative propertyis applied).

Another operation that has been implemented as a variadic function in many languages is output formatting. TheCfunctionprintfand theCommon Lispfunctionformatare two such examples. Both take one argument that specifies the formatting of the output, andany numberof arguments that provide the values to be formatted.

Variadic functions can exposetype-safetyproblems in some languages. For instance, C'sprintf,if used incautiously, can give rise to a class of security holes known asformat 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 thestackthan were placed there, corrupting the stack and leading to unexpected behavior. As a consequence of this, theCERT Coordination Centerconsiders variadic functions in C to be a high-severity security risk.[2]

Infunctional programminglanguages, variadics can be considered complementary to theapplyfunction, 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.[citation needed]In the functional languageHaskell,variadic functions can be implemented by returning a value of atype classT;if instances ofTare a final return valuerand a function(T t) => x -> t,this allows for any number of additional argumentsx.[further explanation needed]

A related subject interm rewritingresearch is calledhedges,orhedge variables.[3]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 themvariadicscan be misleading. However they are referring to the same phenomenon, and sometimes the phrasing is mixed, resulting in names such asvariadic variable(synonymous to hedge). Note the double meaning of the wordvariableand 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).

Examples

[edit]

In C

[edit]

To portably implement variadic functions in theC language,the standardstdarg.hheader file is used. The oldervarargs.hheader has beendeprecatedin favor ofstdarg.h.In C++, the header filecstdargis used.[4]

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

doubleaverage(intcount,...){
va_listap;
intj;
doublesum=0;

va_start(ap,count);/* Before C23: 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);

returnsum/count;
}

intmain(intargc,charconst*argv[]){
printf("%f\n",average(3,1,2,3));
return0;
}

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 beint,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 exampleprintf,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. (Alternatively, asentinel valuelikeNULLmay be used to indicate the number.) 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 theformat string attack.

stdarg.hdeclares a type,va_list,and defines four macros:va_start,va_arg,va_copy,andva_end.Each invocation ofva_startandva_copymust be matched by a corresponding invocation ofva_end.When working with variable arguments, a function normally declares a variable of typeva_list(apin the example) that will be manipulated by the macros.

  1. va_starttakes two arguments, ava_listobject and a reference to the function's last parameter (the one before the ellipsis; the macro uses this to get its bearings). InC23,the second argument will no longer be required and variadic functions will no longer need a named parameter before the ellipsis.[note 1][6]It initialises theva_listobject for use byva_argorva_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_argtakes two arguments, ava_listobject (previously initialised) and a type descriptor. It expands to the next variable argument, and has the specified type. Successive invocations ofva_argallow 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_endtakes one argument, ava_listobject. It serves to clean up. If one wanted to, for instance, scan the variable arguments more than once, the programmer would re-initialise yourva_listobject by invokingva_endand thenva_startagain on it.
  4. va_copytakes two arguments, both of themva_listobjects. 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 invokingva_starton a firstva_list,then usingva_copyto clone it into a secondva_list.After scanning the variable arguments a first time withva_argand the firstva_list(disposing of it withva_end), the programmer could scan the variable arguments a second time withva_argand the secondva_list.va_endneeds to also be called on the clonedva_listbefore the containing function returns.

In C#

[edit]

C#describes variadic functions using theparamskeyword. A type must be provided for the arguments, althoughobject[]can be used as a catch-all. At the calling site, you can either list the arguments one by one, or hand over a pre-existing array having the required element type. Using the variadic form isSyntactic sugarfor the latter.

usingSystem;

classProgram
{
staticintFoo(inta,intb,paramsint[]args)
{
// Return the sum of the integers in args, ignoring a and b.
intsum=0;
foreach(intiinargs)
sum+=i;
returnsum;
}

staticvoidMain(string[]args)
{
Console.WriteLine(Foo(1,2));// 0
Console.WriteLine(Foo(1,2,3,10,20));// 33
int[]manyValues=newint[]{13,14,15};
Console.WriteLine(Foo(1,2,manyValues));// 42
}
}

In C++

[edit]

The basic variadic facility in C++ is largely identical to that in C. The only difference is in the syntax, where the comma before the ellipsis can be omitted. C++ allows variadic functions withoutnamed parametersbut provides no way to access those arguments sinceva_startrequires the name of the last fixed argument of the function.

#include<iostream>
#include<cstdarg>

voidsimple_printf(constchar*fmt...)// C-style "const char* fmt,..." is also valid
{
va_listargs;
va_start(args,fmt);

while(*fmt!='\0'){
if(*fmt=='d'){
inti=va_arg(args,int);
std::cout<<i<<'\n';
}elseif(*fmt=='c'){
// note automatic conversion to integral type
intc=va_arg(args,int);
std::cout<<static_cast<char>(c)<<'\n';
}elseif(*fmt=='f'){
doubled=va_arg(args,double);
std::cout<<d<<'\n';
}
++fmt;
}

va_end(args);
}

intmain()
{
simple_printf("dcff",3,'a',1.999,42.5);
}

Variadic templates(parameter pack) can also be used in C++ with language built-infold expressions.

#include<iostream>

template<typename...Ts>
voidfoo_print(Ts...args)
{
((std::cout<<args<<' '),...);
}

intmain()
{
std::cout<<std::boolalpha;
foo_print(1,3.14f);// 1 3.14
foo_print("Foo",'b',true,nullptr);// Foo b true nullptr
}

TheCERT Coding Standardsfor C++ strongly prefers the use ofvariadic templates(parameter pack) in C++ over the C-style variadic function due to a lower risk of misuse.[7]

In Go

[edit]

Variadic functions inGocan be called with any number of trailing arguments.[8]fmt.Printlnis a common variadic function; it uses an empty interface as a catch-all type.

packagemain

import"fmt"

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

funcmain(){
// 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

In Java

[edit]

As with C#, theObjecttype inJavais available as a catch-all.

publicclassProgram{
// Variadic methods store any additional arguments they receive in an array.
// Consequentially, `printArgs` is actually a method with one parameter: a
// variable-length array of `String`s.
privatestaticvoidprintArgs(String...strings){
for(Stringstring:strings){
System.out.println(string);
}
}

publicstaticvoidmain(String[]args){
printArgs("hello");// short for printArgs([ "hello" ])
printArgs("hello","world");// short for printArgs([ "hello", "world" ])
}
}

In JavaScript

[edit]

JavaScriptdoes not care about types of variadic arguments.

functionsum(...numbers){
returnnumbers.reduce((a,b)=>a+b,0);
}

console.log(sum(1,2,3));// 6
console.log(sum(3,2));// 5
console.log(sum());// 0

It's also possible to create a variadic function using the arguments object, although it is only usable with functions created with thefunctionkeyword.

functionsum(){
returnArray.prototype.reduce.call(arguments,(a,b)=>a+b,0);
}

console.log(sum(1,2,3));// 6
console.log(sum(3,2));// 5
console.log(sum());// 0

Luafunctions may pass varargs to other functions the same way as other values using thereturnkeyword. tables can be passed into variadic functions by using, in Lua version 5.2 or higher[9]table.unpack,or Lua 5.1 or lower[10]unpack.Varargs can be used as a table by constructing a table with the vararg as a value.

functionsum(...)--... designates varargs
localsum=0
for_,vinpairs({...})do--creating a table with a varargs is the same as creating one with standard values
sum=sum+v
end
returnsum
end

values={1,2,3,4}
sum(5,table.unpack(values))--returns 15. table.unpack should go after any other arguments, otherwise not all values will be passed into the function.

functionadd5(...)
return...+5--this is incorrect usage of varargs, and will only return the first value provided
end

entries={}
functionprocess_entries()
localprocessed={}
fori,vinpairs(entries)do
processed[i]=v--placeholder processing code
end
returntable.unpack(processed)--returns all entries in a way that can be used as a vararg
end

print(process_entries())--the print function takes all varargs and writes them to stdout separated by newlines

In Pascal

[edit]

Pascalis standardized byISOstandards 7185 ( “Standard Pascal” ) and 10206 ( “Extended Pascal” ). Neither standardized form of Pascal supports variadic routines,exceptfor certainbuilt-in routines(read/readLnandwrite/writeLn,and additionally inEPreadStr/writeStr).

Nonetheless,dialectsof Pascal implement mechanismsresemblingvariadic routines. Delphidefines anarrayofconstdata type that may be associated with thelastformal parameter. Within the routine definition thearrayofconstis anarrayofTVarRec,anarrayofvariant records.[11] TheVTypemember of the aforementionedrecorddata type allows inspection of the argument’s data type and subsequent appropriate handling. TheFree Pascal Compilersupports Delphi’s variadic routines, too.[12]

This implementation, however, technically requires asingleargument, that is anarray. Pascal imposes the restriction that arrays need to be homogenous. This requirement is circumvented by utilizing a variant record. TheGNU Pascaldefines a real variadic formal parameter specification using an ellipsis (...), but as of 2022 no portable mechanism to use such has been defined.[13]

Both GNU Pascal and FreePascal allow externally declared functions to use a variadic formal parameter specification using an ellipsis (...).

In PHP

[edit]

PHPdoes not care about types of variadic arguments unless the argument is typed.

functionsum(...$nums):int
{
returnarray_sum($nums);
}

echosum(1,2,3);// 6

And typed variadic arguments:

functionsum(int...$nums):int
{
returnarray_sum($nums);
}

echosum(1,'a',3);// TypeError: Argument 2 passed to sum() must be of the type int (since PHP 7.3)

In Python

[edit]

Pythondoes not care about types of variadic arguments.

deffoo(a,b,*args):
print(args)# args is a tuple (immutable sequence).

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

Keyword arguments can be stored in a dictionary, e.g.def bar(*args, **kwargs).

In Raku

[edit]

InRaku,the type of parameters that create variadic functions are known asslurpyarray parameters and they're classified into three groups:

Flattened slurpy

[edit]

These parameters are declared with a single asterisk (*) and they flatten arguments by dissolving one or more layers of elements that can be iterated over (i.e,Iterables).

subfoo($a,$b,*@args) {
say@args.perl;
}

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]

Unflattened slurpy

[edit]

These parameters are declared with two asterisks (**) and they do not flatten any iterable arguments within the list, but keep the arguments more or less as-is:

subbar($a,$b,**@args) {
say@args.perl;
}

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

Contextual slurpy

[edit]

These parameters are declared with a plus (+) sign and they apply the"single argument rule",which decides how to handle the slurpy argument based upon context. Simply put, if only a single argument is passed and that argument is iterable, that argument is used to fill the slurpy parameter array. In any other case,+@works like**@(i.e., unflattened slurpy).

subzaz($a,$b,+@args) {
say@args.perl;
}

zaz(1,2);# []
zaz(1,2,3);# [3]
zaz(1,2,3,"hello");# [3 "hello" ]
zaz(1,2,[4,5]);# [4, 5], single argument fills up array
zaz(1,2,3,[4,5]);# [3, [4, 5]], behaving as **@
zaz(1,2,3,[4,5], [6]);# [3, [4, 5], [6]], behaving as **@

In Ruby

[edit]

Rubydoes not care about types of variadic arguments.

deffoo(*args)
printargs
end

foo(1)
# prints `[1]=> nil`

foo(1,2)
# prints `[1, 2]=> nil`

In Rust

[edit]

Rustdoes not support variadic arguments in functions. Instead, it usesmacros.[14]

macro_rules!calculate{
// The pattern for a single `eval`
(eval$e:expr)=>{{
{
letval:usize=$e;// Force types to be integers
println!("{} = {}",stringify!{$e},val);
}
}};

// Decompose multiple `eval`s recursively
(eval$e:expr,$(eval$es:expr),+)=>{{
calculate!{eval$e}
calculate!{$(eval$es),+}
}};
}

fnmain(){
calculate!{// Look ma! Variadic `calculate!`!
eval1+2,
eval3+4,
eval(2*3)+1
}
}

Rust is able to interact with C's variadic system via ac_variadicfeature switch. As with other C interfaces, the system is consideredunsafeto Rust.[15]

In Scala

[edit]
objectProgram{
// Variadic methods store any additional arguments they receive in an array.
// Consequentially, `printArgs` is actually a method with one parameter: a
// variable-length array of `String`s.
privatedefprintArgs(strings:String*):Unit={
strings.foreach(println)
}

defmain(args:Array[String]):Unit={
printArgs("hello");// short for printArgs([ "hello" ])
printArgs("hello","world");// short for printArgs([ "hello", "world" ])
}
}

In Swift

[edit]

Swiftcares about the type of variadic arguments, but the catch-allAnytype is available.

funcgreet(timeOfTheDay:String,names:String...){
// here, names is [String]

print("Looks like we have\(names.count)people ")

fornameinnames{
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

In Tcl

[edit]

ATclprocedure or lambda is variadic when its last argument isargs:this will contain a list (possibly empty) of all the remaining arguments. This pattern is common in many other procedure-like methods.[16][17]

procgreet{timeOfTheDayargs}{
puts"Looks like we have [llength $args] people"

foreachname$args{
puts"Hello $name, good $timeOfTheDay"
}
}

greet"morning""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

See also

[edit]

Notes

[edit]
  1. ^Making the named parameter optional was needed since there was no way to specify a function taking an unspecified number of arguments in C23 after the removal of K&R style function definitions. Since C++ was already using this syntax for the same purpose, this change was also a way to increase compatibility between the languages.[5]

References

[edit]
  1. ^Henry S. Leonard and H. N. Goodman,A calculus of individuals.Abstract of a talk given at the Second Meeting of the Association for Symbolic Logic, held in Cambridge MA on December 28–30, 1936,[1],Journal of Symbolic Logic2(1) 1937, 63.
  2. ^Klemens, Ben (2014).21st Century C: C Tips from the New School.O'Reilly Media, Inc. p. 224.ISBN978-1491904442.
  3. ^CLP (H): Constraint Logic Programming for Hedges
  4. ^"<cstdarg> (stdarg.h) - C++ Reference".www.cplusplus.com.
  5. ^"C23 is Finished: Here is What is on the Menu §N2975 - Relax requirements for variadic parameter lists".31 July 2022.
  6. ^Gilding, Alex; Meneide, JeanHeyd (2022-04-15)."WG14-N2975: Relax requirements for variadic parameter lists, v3"(PDF).
  7. ^"DCL50-CPP. Do not define a C-style variadic function".
  8. ^"Go by Example: Variadic Functions".
  9. ^"Lua 5.2 Reference Manual".www.lua.org.Retrieved2023-02-05.
  10. ^"Lua 5.1 Reference Manual".www.lua.org.Retrieved2023-02-05.
  11. ^"Parameters (Delphi)".Retrieved2023-08-28.
  12. ^"Free Pascal - Reference guide".Retrieved2023-08-28.
  13. ^"The GNU Pascal Manual".Retrieved2023-08-28.
  14. ^"Variadics".Rust By Example.
  15. ^"2137-variadic".The Rust RFC Book.
  16. ^"proc manual page".Tcl/Tk Documentation.
  17. ^"args".Tcler's Wiki.
[edit]