func_get_args
func_get_args(): array
Collects every argument the current function actually received (the pre-5.6 standard for hand-rolled variadics): even extras beyond the declared parameters are kept; modern code declares ...$args instead, and the two behave identically.
C C23
#include <stdarg.h>
#include <stdio.h>
/* va_list collects the extras; the count (or a sentinel) must come from
the caller, because C varargs carry no length of their own */
static void collect(int n, ...) {
va_list ap;
va_start(ap, n);
printf("count: %d [", n);
for (int i = 0; i < n; i++) {
printf("%d%s", va_arg(ap, int), i + 1 < n ? ", " : "");
}
printf("]\n");
va_end(ap);
}
int main(void) {
collect(3, 10, 20, 30);
return 0;
}
count: 3 [10, 20, 30]
C++ C++26
#include <iostream>
#include <string>
#include <vector>
// a parameter pack collects any arity into one vector: type-safe varargs
template <typename... Ts>
void collect(Ts... args) {
std::vector<std::string> v = {std::to_string(args)...};
std::cout << "count: " << v.size() << " [";
for (size_t i = 0; i < v.size(); i++) {
std::cout << v[i] << (i + 1 < v.size() ? ", " : "");
}
std::cout << "]\n";
}
int main() {
collect(10, 20, 30);
return 0;
}
count: 3 [10, 20, 30]
C# v11.0.100
void Collect(params int[] args) =>
Console.WriteLine($"count: {args.Length} [{string.Join(", ", args)}]");
Collect(10, 20, 30); // the compiler packs loose arguments into the array
count: 3 [10, 20, 30]
Go v1.27.4
package main
import "fmt"
func collect(nums ...int) {
// inside the body a variadic is just a slice: len + the whole list
fmt.Printf("count: %d %v\n", len(nums), nums)
}
func main() {
collect(10, 20, 30)
}
count: 3 [10 20 30]
Java v27.0.1
import java.util.Arrays;
public class Main {
static void collect(int... nums) {
// varargs are an array in the body: length + the whole list
System.out.println("count: " + nums.length + " " + Arrays.toString(nums));
}
public static void main(String[] args) {
collect(10, 20, 30);
}
}
count: 3 [10, 20, 30]
JavaScript v26.13.1
// rest parameters pack everything the caller passed into one real array
function collect(...args) {
console.log(`count: ${args.length}`, args);
}
collect(10, 20, 30);
count: 3 [ 10, 20, 30 ]
PHP v8.6.0
// legacy (pre-5.6): func_get_args collects whatever the caller actually
// passed - even beyond the declared parameters
function legacy($first) { return func_get_args(); }
// modern (5.6+): the ...$args declaration does the same, visibly
function modern(...$args) { return $args; }
echo json_encode(legacy(10, 20, 30)), "\n";
echo json_encode(modern(10, 20, 30)), "\n";
var_dump(legacy(10, 20, 30) === modern(10, 20, 30));
[10,20,30] [10,20,30] bool(true)
Python v3.15.2
def collect(*args):
# *args is a tuple holding everything the caller passed
print("count:", len(args), list(args))
collect(10, 20, 30)
count: 3 [10, 20, 30]
Rust v1.99.1
// no variadic functions in Rust: a slice parameter collects them all
fn collect(nums: &[i32]) {
println!("count: {} {:?}", nums.len(), nums);
}
fn main() {
collect(&[10, 20, 30]);
}
count: 3 [10, 20, 30]
TypeScript v26.13.1
// typed rest parameters pack everything the caller passed into one array
function collect(...args: number[]): void {
console.log(`count: ${args.length}`, args);
}
collect(10, 20, 30);
count: 3 [ 10, 20, 30 ]
Language differences
c: va_list collects the extra arguments, but varargs carry no length of their own - the count (the example's n) or a sentinel must be passed explicitly by the caller; <stdarg.h> is included explicitly (it declares va_start/va_arg/va_end).
cpp: a parameter pack collects any arity into a vector - type-safe, and the count is the pack's size (the example's size()); runtime variadics are still C-style stdarg, don't mix the two.
csharp: params int[] args: the compiler packs loose arguments into an array, and Length is the count (example); no ordinary parameter may follow params (unless it is optional).
go: a variadic ...int is a []int inside the body: len(nums) is the count (example); a slice argument spreads wholesale with a trailing ....
java: varargs int... nums are an array under the hood: length is the count (example); it must sit at the end of the parameter list, one per method at most.
javascript: rest ...args is a real array (not the array-like arguments) - it replaced the arguments object since ES6; args.length is the runtime count (example).
php: func_get_args was the standard answer for hand-rolled variadics in PHP 4-5.5: it collects everything the caller actually passed, extras beyond the declared parameters included (legacy($first) receives 3 in the example); PHP 5.6+'s declarative ...$args built it in, and the two behave identically (the example's === is true). Write ...$args in new code; when reading legacy libraries, recognize func_get_args as the marker of a variadic function. Note it only works inside a function body.
python: *args is a tuple: len(args) is the count (example); keyword variadics go through **kwargs; an existing list spreads with a * prefix (collect(*[10, 20, 30])).
rust: no runtime variadics: a slice &[i32] is the whole collector (the example's len); printf-style needs are expanded at compile time by the format_args! macro.
typescript: same as JavaScript at runtime; at the type level rest can be declared as a named tuple, precisely constraining "the first few parameters fixed, the rest number".
common ground: what collector APIs share - you get a container (array/slice/tuple/list) and the count is the container's length; C's va_list is the sole exception: no container, no length, which is why C variadic functions all carry an explicit count or sentinel parameter.