Variadic function: Difference between revisions

Content deleted Content added
No edit summary
 
(19 intermediate revisions by 14 users not shown)
Line 4:
In [[mathematics]] and in [[computer programming]], a '''variadic function''' is a [[function (programming)|function]] of indefinite [[arity]], i.e., one which accepts a variable number of [[argument (computer science)|argument]]s. Support for variadic functions differs widely among [[programming language]]s.
 
The term ''variadic'' is a [[neologism]], dating back to 1936–19371936/1937.<ref>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, [https://www.jstor.org/stable/2268861], ''Journal of Symbolic Logic'' '''2'''(1) 1937, 63.</ref> The term was not widely used until the 1970s.
 
==Overview==
Line 20:
 
===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|access-date=2007-10-02|archive-date=2012-10-31|archive-url=https://web.archive.org/web/20121031160547/http://www.cplusplus.com/reference/clibrary/cstdarg/|url-status=dead}}</ref>
 
<syntaxhighlight lang="C">
Line 28:
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 (int j = 0; j < count; j++j) {
sum += va_arg(ap, int); /* Increments ap to the next argument. */
}
Line 40 ⟶ 39:
}
 
int main(int argc, char* const *argv[]) {
printf("%f\n", average(3, 1, 2, 3));
return 0;
Line 46 ⟶ 45:
</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 numberend 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.
Line 83 ⟶ 82:
 
===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 parametersparameter]]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++">
import std;
#include <iostream>
#include <cstdarg>
 
void simple_printf(const char* fmt...) // C-style "const char* fmt, ..." is also valid
void simple_printf(const char* fmt...) {
{
va_list args;
va_start(args, fmt);
Line 97 ⟶ 95:
if (*fmt == 'd') {
int i = va_arg(args, int);
std::cout <<println("{}", i << '\n');
} else if (*fmt == 'c') {
// note automatic conversion to integral type
int c = va_arg(args, int);
std::cout <<println("{}", static_cast<char>(c) << '\n');
} else if (*fmt == 'f') {
double d = va_arg(args, double);
std::cout <<println("{}", d << '\n');
}
++fmt;
Line 112 ⟶ 110:
}
 
int main(int argc, char* argv[]) {
{
simple_printf("dcff", 3, 'a', 1.999, 42.5);
}
Line 121 ⟶ 118:
 
<syntaxhighlight lang="c++">
import std;
#include <iostream>
 
template <typename... Ts>
void foo_printfooPrint(Ts... args) {
((std::coutprint("{} <<", args << ' ')), ...);
{
((std::cout << args << ' '), ...);
}
 
int main(int argc, char* argv[]) {
foo_printfooPrint(1, 3.14f); // 1 3.14
{
foo_printfooPrint("Foo", 'b', true, nullptr); // Foo b true nullptr
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
int j;
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===
Line 187 ⟶ 237:
// variable-length array of `String`s.
private static void printArgs(String... strings) {
for (String strings : strings) {
System.out.println(strings);
}
}
Line 297 ⟶ 347:
 
<syntaxhighlight lang="python">
from typing import Any
def foo(a, b, *args):
 
def foo(a: Any, b: Any, *args: Any) -> None:
print(args) # args is a tuple (immutable sequence).
 
if __name__ == "__main__":
foo(1, 2) # ()
foo(1, 2, 3) # (3,)
foo(1, 2, 3, "hello") # (3, "hello")
foo(1, 2, 3, "hello") # (3, "hello")
</syntaxhighlight>
 
Keyword arguments can be stored in a dictionary, e.g. {{code|def bar(*args: Any, **kwargs: Any) -> Any}}.
 
===In Raku===
Line 370 ⟶ 423:
 
=== In Rust ===
[[Rust (programming language)|Rust]] does not support variadic arguments in functions. Instead, it uses [[Macro (computer science)|macros]], which support variadic arguments.<ref>{{cite web |title=Variadics |url=https://doc.rust-lang.org/rust-by-example/macros/variadics.html |website=Rust By Example}}</ref> This is essentially why <code>println!</code> is a macro and not a function, as it takes variadic arguments to format.
 
<syntaxhighlight lang="rust">
Line 390 ⟶ 443:
 
fn main() {
calculate! { // Look ma! Variadic `calculate!`!\
eval 1 + 2,
eval 3 + 4,
Line 480 ⟶ 533:
 
[[Category:Subroutines]]
[[Category:Programming language comparisons]]
<!-- Hidden categories below -->
[[Category:Articles with example C code]]
Line 492 ⟶ 546:
[[Category:Articles with example Ruby code]]
[[Category:Articles with example Rust code]]
[[Category:Articles with example Swift code]]
[[Category:Articles with example Tcl code]]