func_num_args
func_num_args(): int
Returns the number of arguments passed to the current function (pairs with func_get_args for variadic handling).
C C23
#include <stdarg.h>
#include <stdio.h>
/* C varargs carry no count: the caller must pass it explicitly */
static int count_em(int n, ...) { return n; }
int main(void) {
printf("%d\n", count_em(3, "a", "b", "c"));
return 0;
}
3
C++ C++26
#include <iostream>
// sizeof... counts a template parameter pack at compile time
template <typename... Ts>
constexpr int count_em(Ts... args) { return sizeof...(args); }
int main() {
std::cout << count_em("a", "b", "c") << "\n";
return 0;
}
3
C# v11.0.100
// params: the callee sees one array; Length is the count
static int CountEm(params object[] args) => args.Length;
Console.WriteLine(CountEm("a", "b", "c"));
3
Go v1.27.4
package main
import "fmt"
func countEm(args ...any) int { return len(args) }
func main() {
fmt.Println(countEm("a", "b", "c"))
}
3
Java v27.0.1
public class Main {
static int countEm(Object... args) { return args.length; }
public static void main(String[] args) {
System.out.println(countEm("a", "b", "c"));
}
}
3
JavaScript v26.13.1
function countEm(...args) { return args.length; }
console.log(countEm("a", "b", "c"), countEm.length); // runtime count vs declared arity
3 0
PHP v8.6.0
function countEm(...$args) {
return [func_num_args(), func_get_args()]; // count and values, both at runtime
}
echo json_encode(countEm('a', 'b', 'c'));
[3,["a","b","c"]]
Python v3.15.2
def count_em(*args):
return len(args)
print(count_em("a", "b", "c"))
3
Rust v1.99.1
// no variadic functions (macros aside); a slice parameter is the idiom
fn count_em(args: &[&str]) -> usize {
args.len()
}
fn main() {
println!("{}", count_em(&["a", "b", "c"]));
}
3
TypeScript v26.13.1
function countEm(...args: string[]): number { return args.length; }
console.log(countEm("a", "b", "c"), countEm.length); // runtime count vs declared arity
3 0
Language differences
C: the varargs mechanism carries no count — at runtime you cannot ask "how many did I get"; conventions are all you have (printf's format string, the explicit count in the example); reading past the real count is undefined behavior.
C++: a template parameter pack's sizeof... yields the count at compile time (example), constant-folded per call site; runtime variadics remain C-style stdarg, equally unsafe.
C#: params packs arguments into an array and Length is the count (example); extra arguments never misalign — safety comes from the type system.
Go: variadics arrive in the function body as one slice, and len is the count (example); use ...any for mixed-type lists.
Java: varargs are an array and args.length is the count (example); the compiler boxes loose arguments into the array.
JavaScript: rest-parameter args.length is the runtime count (example); by contrast countEm.length is the DECLARED arity (rest parameters count as 0) — don't mix the two numbers.
PHP: func_num_args/func_get_args were the PHP 4 staple for hand-rolled variadics; PHP 5.6+ built them in as ...$args (example) — count($args) equals func_num_args(), and new code should prefer rest syntax.
Python: *args is a tuple and len is the count (example); keyword variadics use **kwargs.
Rust: no runtime variadics — the slice's len (example) is the whole story; printf-like needs are expanded at compile time by the format_args! macro.
TypeScript: same as JavaScript; countEm.length has precise meaning at the type level (counting optional parameters), matching JS at runtime.