Content deleted Content added
Undid revision 1294251833 by 2600:387:8:9:0:0:0:59 (talk) because it removed a </syntaxhighlight> tag, causing the rest of the section on C and the section on C# to be incorporated into the C language example. |
No edit summary |
||
(One intermediate revision by one other user not shown) | |||
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;
double sum = 0;
va_start(ap, count); /
for (int j = 0; j < count;
sum += va_arg(ap, int); /* Increments ap to the next argument. */
}
Line 40 ⟶ 39:
}
int main(int argc, char*
printf("%f\n", average(3, 1, 2, 3));
return 0;
Line 86 ⟶ 85:
<syntaxhighlight lang="c++">
import std;
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::
} else if (*fmt == 'c') {
// note automatic conversion to integral type
int c = va_arg(args, int);
std::
} else if (*fmt == 'f') {
double d = va_arg(args, double);
std::
}
++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;
template <typename... Ts>
void
▲ ((std::cout << args << ' '), ...);
}
int main(int argc, char* argv[]) {
▲ foo_print(1, 3.14f); // 1 3.14
▲ foo_print("Foo", 'b', true, nullptr); // Foo b true nullptr
}
</syntaxhighlight>
Line 243 ⟶ 237:
// variable-length array of `String`s.
private static void printArgs(String... strings) {
for (String
System.out.println(
}
}
Line 353 ⟶ 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
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 426 ⟶ 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 446 ⟶ 443:
fn main() {
calculate! {
eval 1 + 2,
eval 3 + 4,
|