call_user_func_array
call_user_func_array(callable $callback, array $args): mixed
Invokes a callback with arguments unpacked from an array (the classic entry point for variadic calls).
C C23
#include <stdarg.h>
#include <stdio.h>
/* C varargs carry no count: the caller must say how many */
static int add(int count, ...) {
va_list ap;
va_start(ap, count);
int sum = 0;
for (int i = 0; i < count; i++) sum += va_arg(ap, int);
va_end(ap);
return sum;
}
int main(void) {
printf("%d\n", add(4, 1, 2, 3, 4)); /* count first, then the values */
return 0;
}
10
C++ C++26
#include <iostream>
// variadic template: sizeof... counts the pack, the fold sums it
template <typename... Ts>
int add(Ts... ns) { return (0 + ... + ns); }
int main() {
std::cout << add(1, 2, 3, 4) << "\n";
return 0;
}
10
C# v11.0.100
// params: callers may pass loose values or a whole array
static int Add(params int[] nums) => nums.Sum();
Console.WriteLine(Add(1, 2, 3, 4));
Console.WriteLine(Add(new[] { 1, 2, 3, 4 }));
10 10
Go v1.27.4
package main
import "fmt"
func add(nums ...int) int {
total := 0
for _, n := range nums {
total += n
}
return total
}
func main() {
nums := []int{1, 2, 3, 4}
fmt.Println(add(1, 2, 3, 4), add(nums...)) // nums... spreads the slice
}
10 10
Java v27.0.1
public class Main {
static int add(int... nums) {
int total = 0;
for (int n : nums) total += n;
return total;
}
public static void main(String[] args) {
System.out.println(add(1, 2, 3, 4) + " " + add(new int[] {1, 2, 3, 4}));
}
}
10 10
JavaScript v26.13.1
const add = (...nums) => nums.reduce((a, b) => a + b, 0);
console.log(add(1, 2, 3, 4), add(...[1, 2, 3, 4])); // ... spreads the array at the call site
10 10
PHP v8.6.0
function add(int ...$nums): int { return array_sum($nums); }
echo call_user_func_array('add', [1, 2, 3, 4]), PHP_EOL; // classic: callback + args array
echo add(...[1, 2, 3, 4]), PHP_EOL; // modern: ... spread
10 10
Python v3.15.2
def add(*nums):
return sum(nums)
# * unpacks a list into positional arguments
print(add(1, 2, 3, 4), add(*[1, 2, 3, 4]))
10 10
Rust v1.99.1
// no variadic functions (macros aside); a slice parameter is the idiom
fn add(nums: &[i32]) -> i32 {
nums.iter().sum()
}
fn main() {
let args = [1, 2, 3, 4];
println!("{}", add(&args)); // passing the slice is the closest spread
}
10
TypeScript v26.13.1
const add = (...nums: number[]): number => nums.reduce((a, b) => a + b, 0);
const args: number[] = [1, 2, 3, 4];
console.log(add(1, 2, 3, 4), add(...args)); // ... spreads the array at the call site
10 10
Language differences
C: variadics only come via stdarg.h, and the count must be carried explicitly by the caller (the count parameter in the example) — va_arg has no type checking and a mismatched count is undefined behavior.
C++: variadic templates + fold expressions (example) are the type-safe modern answer; C++17's std::apply(f, tuple) maps exactly to "function + argument pack".
C#: params arrays serve both call styles (example) — loose values or a whole array; since C# 12, collection expressions Add([1, 2, 3, 4]) come even closer to array spreading.
Go: nums... is language-level spreading (example) — semantically the closest to call_user_func_array; the slice type must match the parameter exactly.
Java: varargs compile to an array, so passing the array directly IS the argument-array call (example); there is no true call-site spread syntax.
JavaScript: the f(...arr) spread (example) is the modern call_user_func_array; f.apply(null, arr) is its historical form.
PHP: call_user_func_array was the only array-spread entry in the PHP 5.3 era; since PHP 5.6 prefer add(...$args) (both shown in the example) — though call_user_func_array remains irreplaceable for by-reference parameters.
Python: f(*args) star-unpacking (example); dicts use f(**kwargs); the unpack operators are core language features with no separate function.
Rust: no variadic functions (macros aside) — the idiom is a slice parameter (example), turning "spread an array into a call" into "pass one slice"; write a macro when true variadics are needed.
TypeScript: same as JavaScript; tuple types + spread give a type-safe call_user_func_array: f(...(args as Parameters<typeof f>)).