call_user_func

call_user_func(callable $callback, mixed ...$args): mixed

Invokes a callback (call by name; closures work the same way).

C C23

C23
#include <stdio.h>

static int double_it(int n) { return n * 2; }

int main(void) {
    /* first-class behavior = function pointer */
    int (*fp)(int) = double_it;
    printf("%d\n", fp(21));
    return 0;
}
Output
42

C++ C++26

C++26
#include <functional>
#include <iostream>

int main() {
    // std::function type-erases callables: lambdas, function pointers, functors
    std::function<int(int)> f = [](int n) { return n * 2; };
    std::cout << f(21) << "\n";
    return 0;
}
Output
42

C# v11.0.100

v11.0.100
// delegates / Func<T>: typed first-class function references
Func<int, int> f = n => n * 2;
Console.WriteLine(f(21));
Output
42

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
	// functions are values: assign, pass, store
	f := func(n int) int { return n * 2 }
	fmt.Println(f(21))
}
Output
42

Java v27.0.1

v27.0.1
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        // no function pointers; a functional interface + lambda plays the role
        Function<Integer, Integer> f = n -> n * 2;
        System.out.println(f.apply(21));
    }
}
Output
42

JavaScript v26.13.1

v26.13.1
// first-class citizens: assign, pass, call like any value
const f = (n) => n * 2;
console.log(f(21), f.call(null, 21)); // .call is the closest cousin of call_user_func
Output
42 42

PHP v8.6.0

v8.6.0
function double($n) { return $n * 2; }
echo call_user_func('double', 21), PHP_EOL; // invoke by string name
echo call_user_func(fn($n) => $n + 1, 41), PHP_EOL; // closures work the same way
Output
42
42

Python v3.15.2

v3.15.2
def double(n):
    return n * 2

# functions are objects; globals()[name] mirrors call-by-string
print(double(21), globals()['double'](21))
Output
42 42

Rust v1.99.1

v1.99.1
fn main() {
    // closures implement the Fn family of traits
    let f = |n: i32| n * 2;
    println!("{}", f(21));
}
Output
42

TypeScript v26.13.1

v26.13.1
// first-class citizens: assign, pass, call like any value
const f = (n: number): number => n * 2;
console.log(f(21), f.call(undefined, 21));
Output
42 42

Language differences

C: the only form of first-class functions is the function pointer (example); signatures must match exactly and there is no type erasure — passing callbacks to APIs like qsort is this road taken to its end.

C++: std::function provides type erasure (example), unifying lambdas, function pointers, functors, and bind results; in performance-sensitive code prefer template parameters or auto parameters and avoid std::function's heap allocation.

C#: delegates are language-level first-class functions (example): generic Func<T>/Action<T> types + lambda assignment; multicast delegates can even hold several targets at once.

Go: functions are first-class values (example) but there is no call-by-name — use a map[string]func(...) registry when a name-to-function mapping is needed.

Java: no function pointers; the java.util.function interface family + lambdas (example) emulate callbacks; method references like Main::doubleIt read even better.

JavaScript: functions are first-class citizens (example), and f.call/f.apply are the closest semantic cousins of call_user_func; calling by string name is possible via globalThis[name] but is an anti-pattern.

PHP: call_user_func accepts a string function name, an [object, 'method'] array, or a closure (example); modern code prefers direct $f(...) syntax or callable type declarations, keeping call_user_func for truly dynamic invocation.

Python: functions are objects (example) and you just call them; globals()[name] mirrors call-by-string, and operator.methodcaller/getattr(obj, name) cover method-name scenarios.

Rust: closures are anonymous structures implementing the Fn/FnMut/FnOnce traits (example); function items coerce to fn pointers, and generic callbacks prefer impl Fn bounds.

TypeScript: same as JavaScript; at the type level constrain callback signatures with () => T function types.