Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Variadic function
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Examples== ===In C=== To portably implement variadic functions in the [[C (programming language)|C language]], the standard [[stdarg.h|{{code|stdarg.h}}]] header file is used. The older [[varargs.h|{{code|varargs.h}}]] header has been [[Deprecation|deprecated]] in favor of {{code|stdarg.h}}. In C++, the header file {{code|cstdarg}} is used.<ref>{{cite web|url=http://www.cplusplus.com/reference/clibrary/cstdarg/|title=<cstdarg> (stdarg.h) - C++ Reference|website=www.cplusplus.com}}</ref> <syntaxhighlight lang="C"> #include <stdarg.h> #include <stdio.h> double average(int count, ...) { va_list ap; int j; double sum = 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); return sum / count; } int main(int argc, char const *argv[]) { printf("%f\n", average(3, 1, 2, 3)); return 0; } </syntaxhighlight> 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 {{code|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. (Alternatively, a [[sentinel value]] like {{code|NULL}} or {{code|nullptr}} may be used to indicate the end of the parameter list.) 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]]. Depending on the system, even using {{code|NULL}} as a sentinel may encounter such problems; {{code|nullptr}} or a dedicated null pointer of the correct target type may be used to avoid them. {{code|stdarg.h}} declares a type, {{code|va_list}}, and defines four macros: [[va start|{{code|va_start}}]], [[va arg|{{code|va_arg}}]], [[va copy|{{code|va_copy}}]], and [[va end|{{code|va_end}}]]. Each invocation of {{code|va_start}} and {{code|va_copy}} must be matched by a corresponding invocation of {{code|va_end}}. When working with variable arguments, a function normally declares a variable of type {{code|va_list}} ({{code|ap}} in the example) that will be manipulated by the macros. # {{code|va_start}} takes two arguments, a {{code|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). In [[C23 (C standard revision)|C23]], the second argument will no longer be required and variadic functions will no longer need a named parameter before the ellipsis.{{r|g=note|n=KandR|r=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.<ref>{{cite web|url=https://thephd.dev/c23-is-coming-here-is-what-is-on-the-menu#n2975---relax-requirements-for-variadic-parameter-lists|title=C23 is Finished: Here is What is on the Menu §N2975 - Relax requirements for variadic parameter lists|date=31 July 2022 }}</ref>}}<ref name=N2975>{{cite web|url=https://open-std.org/JTC1/SC22/WG14/www/docs/n2975.pdf|title=WG14-N2975 : Relax requirements for variadic parameter lists, v3|date=2022-04-15|last1=Gilding|first1=Alex|last2=Meneide|first2=JeanHeyd}}</ref> It initialises the {{code|va_list}} object for use by {{code|va_arg}} or {{code|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. # {{code|va_arg}} takes two arguments, a {{code|va_list}} object (previously initialised) and a type descriptor. It expands to the next variable argument, and has the specified type. Successive invocations of {{code|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. # {{code|va_end}} takes one argument, a {{code|va_list}} object. It serves to clean up. If one wanted to, for instance, scan the variable arguments more than once, the programmer would re-initialise your {{code|va_list}} object by invoking {{code|va_end}} and then {{code|va_start}} again on it. # {{code|va_copy}} takes two arguments, both of them {{code|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 {{code|va_start}} on a first {{code|va_list}}, then using {{code|va_copy}} to clone it into a second {{code|va_list}}. After scanning the variable arguments a first time with {{code|va_arg}} and the first {{code|va_list}} (disposing of it with {{code|va_end}}), the programmer could scan the variable arguments a second time with {{code|va_arg}} and the second {{code|va_list}}. {{code|va_end}} needs to also be called on the cloned {{code|va_list}} before the containing function returns. ===In C#=== [[C Sharp (programming language)|C#]] describes variadic functions using the {{code|params}} keyword. A type must be provided for the arguments, although {{code|object[]}} 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 is [[Syntactic sugar]] for the latter. <syntaxhighlight lang="c#" highlight="5,16-17,19"> 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 int[] manyValues = new int[] { 13, 14, 15 }; Console.WriteLine(Foo(1, 2, manyValues)); // 42 } } </syntaxhighlight> ===In C++=== 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 without [[named parameter]]s but provides no way to access those arguments since <code>va_start</code> requires the name of the last fixed argument of the function. <!-- When C23 is released the text should be updated to reflect that this is a difference between the languages. When C++26 ports the one argument va_start to C++ (https://wg21.link/p2537) this should be updated again. See also the C section of the article. --> <syntaxhighlight lang="c++"> #include <iostream> #include <cstdarg> void simple_printf(const char* fmt...) // C-style "const char* fmt, ..." is also valid { va_list args; va_start(args, fmt); while (*fmt != '\0') { if (*fmt == 'd') { int i = va_arg(args, int); std::cout << i << '\n'; } else if (*fmt == 'c') { // note automatic conversion to integral type int c = va_arg(args, int); std::cout << static_cast<char>(c) << '\n'; } else if (*fmt == 'f') { double d = va_arg(args, double); std::cout << d << '\n'; } ++fmt; } va_end(args); } int main() { simple_printf("dcff", 3, 'a', 1.999, 42.5); } </syntaxhighlight> [[Variadic templates]] (parameter pack) can also be used in C++ with language built-in [[Fold (higher-order function)|fold expressions]]. <syntaxhighlight lang="c++"> #include <iostream> template <typename... Ts> void foo_print(Ts... args) { ((std::cout << args << ' '), ...); } int main() { std::cout << std::boolalpha; foo_print(1, 3.14f); // 1 3.14 foo_print("Foo", 'b', true, nullptr); // Foo b true nullptr } </syntaxhighlight> The [[CERT Coding Standards]] for C++ strongly prefers the use of [[variadic templates]] (parameter pack) in C++ over the C-style variadic function due to a lower risk of misuse.<ref>{{cite web |title=DCL50-CPP. Do not define a C-style variadic function |url=https://wiki.sei.cmu.edu/confluence/display/cplusplus/DCL50-CPP}}</ref> === In Fortran === Since the Fortran 90 revision, [[Fortran]] functions or subroutines can accept optional arguments:<ref>{{Cite web |title=Optional Arguments |url=https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-0/optional-arguments.html |access-date=2025-03-18 |website=Intel |language=en}}</ref> the argument list is still fixed, but the ones that have the {{code|optional}} attribute can be omitted in the function/subroutine call. The intrinsic function {{code|present()}} can be used to detect the presence of an optional argument. The optional arguments can appear anywhere in the argument list. <syntaxhighlight lang="fortran">program test implicit none real :: x !> all arguments are passed: call foo( 1, 2, 3.0, 4, x ) !< outputs 1 \ 2 \ 3.0 \ 4 \ 6.0 (the "\" denotes a newline) !> the last 2 arguments are omitted: call foo( 1, 2, 3.0 ) !< outputs 1 \ 2 \ 3.0 !> the 2nd and 4th arguments are omitted: the arguments that are positioned after !> an omitted argument must be passed with a keyword: call foo( 1, c=3.0, e=x ) !< outputs 1 \ 3.0 \ 6.0 !> alternatively, the Fortran 2023 revision has introduced the .NIL. pseudo constant !> to denote an omitted argument call foo( 1, .NIL., 3.0, .NIL., x ) !< outputs 1 \ 3.0 \ 6.0 contains !> the subroutine foo() has 2 mandatory and 3 optional arguments subroutine foo( a, b, c, d, e ) integer, intent(in) :: a integer, intent(in), optional :: b real, intent(in) :: c integer, intent(in), optional :: d real, intent(out), optional :: e print*, a if (present(b)) print*, b print*, c if (present(d)) print*, d if (present(e)) then e = 2*c print*, c end if end subroutine end program</syntaxhighlight> '''Output:''' <pre> The sum of [1 2] is 3 The sum of [1 2 3] is 6 The sum of [1 2 3 4] is 10 </pre> ===In Go=== Variadic functions in [[Go (programming language)|Go]] can be called with any number of trailing arguments.<ref>{{Cite web|url=https://gobyexample.com/variadic-functions|title=Go by Example: Variadic Functions}}</ref> {{code|fmt.Println}} is a common variadic function; it uses an empty interface as a catch-all type. <syntaxhighlight lang="go"> 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" } </syntaxhighlight> '''Output:''' <pre> The sum of [1 2] is 3 The sum of [1 2 3] is 6 The sum of [1 2 3 4] is 10 </pre> ===In Java=== As with C#, the {{code|Object}} type in [[Java (programming language)|Java]] is available as a catch-all. <syntaxhighlight lang="java"> public class Program { // 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. private static void printArgs(String... strings) { for (String string : strings) { System.out.println(string); } } public static void main(String[] args) { printArgs("hello"); // short for printArgs(["hello"]) printArgs("hello", "world"); // short for printArgs(["hello", "world"]) } } </syntaxhighlight> === In JavaScript === [[JavaScript]] does not care about types of variadic arguments. <syntaxhighlight lang="javascript"> function sum(...numbers) { return numbers.reduce((a, b) => a + b, 0); } console.log(sum(1, 2, 3)); // 6 console.log(sum(3, 2)); // 5 console.log(sum()); // 0 </syntaxhighlight> It's also possible to create a variadic function using the arguments object, although it is only usable with functions created with the {{code|function}} keyword. <syntaxhighlight lang="javascript"> function sum() { return Array.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 </syntaxhighlight> === In [[Lua (programming language)|Lua]] === [[Lua (programming language)|Lua]] functions may pass varargs to other functions the same way as other values using the {{Code|return}} keyword. tables can be passed into variadic functions by using, in Lua version 5.2 or higher<ref>{{Cite web |title=Lua 5.2 Reference Manual |url=https://www.lua.org/manual/5.2/manual.html#pdf-table.unpack |access-date=2023-02-05 |website=www.lua.org}}</ref> {{Code|table.unpack}}, or Lua 5.1 or lower<ref>{{Cite web |title=Lua 5.1 Reference Manual |url=https://www.lua.org/manual/5.1/manual.html#pdf-unpack |access-date=2023-02-05 |website=www.lua.org}}</ref> {{Code|unpack}}. Varargs can be used as a table by constructing a table with the vararg as a value.<syntaxhighlight lang="lua"> function sum(...) --... designates varargs local sum=0 for _,v in pairs({...}) do --creating a table with a varargs is the same as creating one with standard values sum=sum+v end return sum 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. function add5(...) return ...+5 --this is incorrect usage of varargs, and will only return the first value provided end entries={} function process_entries() local processed={} for i,v in pairs(entries) do processed[i]=v --placeholder processing code end return table.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 </syntaxhighlight> ===In Pascal=== [[Pascal (programming language)|Pascal]] is standardized by [[International Organization for Standardization|ISO]] standards 7185 (“Standard Pascal”) and 10206 (“Extended Pascal”). Neither standardized form of Pascal supports variadic routines, ''except'' for certain [[Intrinsic function|built-in routines]] ({{code|read|pascal}}/{{code|readLn|pascal}} and {{code|write|pascal}}/{{code|writeLn|pascal}}, and additionally in {{abbr|EP|Extended Pascal}} {{code|readStr|pascal}}/{{code|writeStr|pascal}}). Nonetheless, ''dialects'' of Pascal implement mechanisms ''resembling'' variadic routines. [[Delphi (programming language)|Delphi]] defines an {{code|array of const|delphi}} data type that may be associated with the ''last'' [[formal parameter]]. Within the routine definition the {{code|array of const|delphi}} is an {{code|array of TVarRec|delphi}}, an [[Array (data type)|array]] of [[variant record]]s.<ref name="delphi">{{cite web|url=https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Parameters_(Delphi)#Variant_Open_Array_Parameters|title=Parameters (Delphi)|access-date=2023-08-28}}</ref> The {{code|VType|delphi}} member of the aforementioned {{code|record|delphi}} data type allows inspection of the argument’s data type and subsequent appropriate handling. The [[Free Pascal Compiler]] supports Delphi’s variadic routines, too.<ref name="fpc">{{cite web|url=https://www.freepascal.org/docs-html/3.2.0/ref/refsu68.html|title=Free Pascal - Reference guide|access-date=2023-08-28}}</ref> This implementation, however, technically requires a ''single'' argument, that is an {{code|array|pascal}}. Pascal imposes the restriction that arrays need to be homogenous. This requirement is circumvented by utilizing a variant record. The [[GNU Pascal]] defines a real variadic formal parameter specification using an ellipsis ({{code|...|pascal}}), but as of 2022 no portable mechanism to use such has been defined.<ref name="gpc">{{cite web|url=https://www.gnu-pascal.de/gpc/Special-Parameters.html|title=The GNU Pascal Manual|access-date=2023-08-28}}</ref> Both GNU Pascal and FreePascal allow externally declared functions to use a variadic formal parameter specification using an ellipsis ({{code|...|pascal}}). ===In PHP=== [[PHP]] does not care about types of variadic arguments unless the argument is typed. <syntaxhighlight lang="php"> function sum(...$nums): int { return array_sum($nums); } echo sum(1, 2, 3); // 6 </syntaxhighlight> And typed variadic arguments: <syntaxhighlight lang="php"> function sum(int ...$nums): int { return array_sum($nums); } echo sum(1, 'a', 3); // TypeError: Argument 2 passed to sum() must be of the type int (since PHP 7.3) </syntaxhighlight> ===In Python=== [[Python (programming language)|Python]] does not care about types of variadic arguments. <syntaxhighlight lang="python"> def foo(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") </syntaxhighlight> Keyword arguments can be stored in a dictionary, e.g. {{code|def bar(*args, **kwargs)}}. ===In Raku=== In [[Raku (programming language)|Raku]], the type of parameters that create variadic functions are known as ''slurpy'' array parameters and they're classified into three groups: ====Flattened slurpy==== These parameters are declared with a single asterisk (<code>*</code>) and they flatten arguments by dissolving one or more layers of elements that can be iterated over (i.e, [https://docs.perl6.org/type/Iterable Iterables]). <syntaxhighlight lang="perl6"> sub foo($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] </syntaxhighlight> ====Unflattened slurpy==== These parameters are declared with two asterisks (<code>**</code>) and they do not flatten any iterable arguments within the list, but keep the arguments more or less as-is: <syntaxhighlight lang="perl6"> sub bar($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]] </syntaxhighlight> ====Contextual slurpy==== These parameters are declared with a plus (<code>+</code>) sign and they apply the ''"[https://docs.perl6.org/language/functions#Slurpy_conventions 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, <code>+@</code> works like <code>**@</code> (i.e., unflattened slurpy). <syntaxhighlight lang="perl6"> sub zaz($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 **@ </syntaxhighlight> ===In Ruby=== [[Ruby (programming language)|Ruby]] does not care about types of variadic arguments. <syntaxhighlight lang="ruby"> def foo(*args) print args end foo(1) # prints `[1]=> nil` foo(1, 2) # prints `[1, 2]=> nil` </syntaxhighlight> === In Rust === [[Rust (programming language)|Rust]] does not support variadic arguments in functions. Instead, it uses [[Macro (computer science)|macros]].<ref>{{cite web |title=Variadics |url=https://doc.rust-lang.org/rust-by-example/macros/variadics.html |website=Rust By Example}}</ref> <syntaxhighlight lang="rust"> macro_rules! calculate { // The pattern for a single `eval` (eval $e:expr) => {{ { let val: 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),+ } }}; } fn main() { calculate! { // Look ma! Variadic `calculate!`! eval 1 + 2, eval 3 + 4, eval (2 * 3) + 1 } } </syntaxhighlight> Rust is able to interact with C's variadic system via a {{code|c_variadic}} feature switch. As with other C interfaces, the system is considered {{code|unsafe}} to Rust.<ref>{{cite web |title=2137-variadic |url=https://rust-lang.github.io/rfcs/2137-variadic.html |website=The Rust RFC Book}}</ref> === In Scala === <syntaxhighlight lang="scala"> object Program { // 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. private def printArgs(strings: String*): Unit = { strings.foreach(println) } def main(args: Array[String]): Unit = { printArgs("hello"); // short for printArgs(["hello"]) printArgs("hello", "world"); // short for printArgs(["hello", "world"]) } } </syntaxhighlight> ===In Swift=== [[Swift (programming language)|Swift]] cares about the type of variadic arguments, but the catch-all {{code|Any}} type is available. <syntaxhighlight lang="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 </syntaxhighlight> ===In Tcl=== A [[Tcl]] procedure or lambda is variadic when its last argument is {{code|args}}: this will contain a list (possibly empty) of all the remaining arguments. This pattern is common in many other procedure-like methods.<ref>{{cite web |title=proc manual page |url=https://www.tcl.tk/man/tcl/TclCmd/proc.html |website=Tcl/Tk Documentation}}</ref><ref>{{cite web |title=args |url=https://wiki.tcl-lang.org/page/args |website=Tcler's Wiki}}</ref> <syntaxhighlight lang="tcl"> proc greet {timeOfTheDay args} { puts "Looks like we have [llength $args] people" foreach name $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 </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)