Named Arguments

Position or name — PHP 8.0 named arguments versus how 10 languages pass parameters.

C C23

C23
#include <stdio.h>
#include <string.h>

/* C has no named arguments in calls; designated initializers (C99) make the
   struct initializer itself "named" — the nearest analogue */
struct Options {
    const char *kind;
    const char *size;
};

static char buf[64];

const char *make_coffee(struct Options o) {
    const char *kind = o.kind ? o.kind : "cappuccino";
    const char *size = o.size ? o.size : "medium";
    snprintf(buf, sizeof(buf), "%s %s", size, kind);
    return buf;
}

int main(void) {
    printf("%s\n", make_coffee((struct Options){.size = "large"}));
    printf("%s\n", make_coffee((struct Options){.kind = "espresso", .size = "small"}));
    printf("%s\n", make_coffee((struct Options){0}));
    return 0;
}
Output
large cappuccino
small espresso
medium cappuccino

C++ C++26

C++26
#include <cstdio>
#include <string>

// C++ has no named arguments; designated initializers (C++20) give struct
// literals the same "named" flavor as C99
struct Options {
    std::string kind;
    std::string size;
};

std::string make_coffee(const Options &o) {
    std::string kind = o.kind.empty() ? "cappuccino" : o.kind;
    std::string size = o.size.empty() ? "medium" : o.size;
    return size + " " + kind;
}

int main() {
    std::printf("%s\n", make_coffee(Options{.size = "large"}).c_str());
    std::printf("%s\n", make_coffee(Options{.kind = "espresso", .size = "small"}).c_str());
    std::printf("%s\n", make_coffee(Options{}).c_str());
    return 0;
}
Output
large cappuccino
small espresso
medium cappuccino

C# v11.0.100

v11.0.100
string MakeCoffee(string kind = "cappuccino", string size = "medium") => $"{size} {kind}";

Console.WriteLine(MakeCoffee(size: "large"));                   // named arguments (C# 4+)
Console.WriteLine(MakeCoffee(kind: "espresso", size: "small")); // order-free
Console.WriteLine(MakeCoffee("latte", size: "large"));          // positional first, named after
Output
large cappuccino
small espresso
large latte

Go v1.27.4

v1.27.4
package main

import "fmt"

// Go has no named arguments: a config struct literal is the idiom —
// field names at the call site play the role of named arguments
type Options struct {
	Kind string
	Size string
}

func makeCoffee(o Options) string {
	kind := o.Kind
	if kind == "" {
		kind = "cappuccino"
	}
	size := o.Size
	if size == "" {
		size = "medium"
	}
	return size + " " + kind
}

func main() {
	fmt.Println(makeCoffee(Options{Size: "large"}))
	fmt.Println(makeCoffee(Options{Kind: "espresso", Size: "small"}))
	fmt.Println(makeCoffee(Options{}))
}
Output
large cappuccino
small espresso
medium cappuccino

Java v27.0.1

v27.0.1
public class Main {
    // Java has no named arguments: the Builder pattern documents call sites
    record Coffee(String kind, String size) {
        static class Builder {
            private String kind = "cappuccino";
            private String size = "medium";
            Builder kind(String k) { this.kind = k; return this; }
            Builder size(String s) { this.size = s; return this; }
            Coffee build() { return new Coffee(kind, size); }
        }
    }

    public static void main(String[] args) {
        System.out.println(new Coffee.Builder().size("large").build());
        System.out.println(new Coffee.Builder().kind("espresso").size("small").build());
    }
}
Output
Coffee[kind=cappuccino, size=large]
Coffee[kind=espresso, size=small]

JavaScript v26.13.1

v26.13.1
// JS has no named arguments: the idiom is destructuring an options object
function makeCoffee({ kind = "cappuccino", size = "medium" } = {}) {
  return `${size} ${kind}`;
}

console.log(makeCoffee({ size: "large" }));                   // skip defaults freely
console.log(makeCoffee({ kind: "espresso", size: "small" })); // self-documenting
console.log(makeCoffee({}));                                  // all defaults
Output
large cappuccino
small espresso
medium cappuccino

PHP v8.6.0

v8.6.0
function makeCoffee(string $type = "cappuccino", string $size = "medium"): string {
    return $size . " " . $type;
}

echo makeCoffee(size: "large"), "\n";                   // skip $type's default entirely
echo makeCoffee(type: "espresso", size: "small"), "\n"; // self-documenting at the call site
echo makeCoffee("latte", size: "large"), "\n";          // positional first, named after
Output
large cappuccino
small espresso
large latte

Python v3.15.2

v3.15.2
def make_coffee(kind="cappuccino", size="medium"):
    return f"{size} {kind}"

print(make_coffee(size="large"))                    # keyword arguments: native since 1.0
print(make_coffee(kind="espresso", size="small"))  # order-free
print(make_coffee("latte", size="large"))           # positional first, keyword after
Output
large cappuccino
small espresso
large latte

Rust v1.99.1

v1.99.1
// Rust has no named arguments: a struct literal (with defaults via Default)
// plays the same role
#[derive(Default)]
struct Options {
    kind: String,
    size: String,
}

impl Options {
    fn kind(mut self, k: &str) -> Self { self.kind = k.to_string(); self }
    fn size(mut self, s: &str) -> Self { self.size = s.to_string(); self }
}

fn make_coffee(o: &Options) -> String {
    let kind = if o.kind.is_empty() { "cappuccino" } else { o.kind.as_str() };
    let size = if o.size.is_empty() { "medium" } else { o.size.as_str() };
    format!("{} {}", size, kind)
}

fn main() {
    println!("{}", make_coffee(&Options::default().size("large".into())));
    println!("{}", make_coffee(&Options::default().kind("espresso").size("small".into())));
    println!("{}", make_coffee(&Options::default()));
}
Output
large cappuccino
small espresso
medium cappuccino

TypeScript v26.13.1

v26.13.1
// TS has no named arguments: an options object with destructured defaults
// is the idiom, plus a type for the shape
interface Options {
  kind?: string;
  size?: string;
}

function makeCoffee({ kind = "cappuccino", size = "medium" }: Options = {}): string {
  return `${size} ${kind}`;
}

console.log(makeCoffee({ size: "large" }));
console.log(makeCoffee({ kind: "espresso", size: "small" }));
console.log(makeCoffee({}));
Output
large cappuccino
small espresso
medium cappuccino

Language differences

C: calls are positional only; C99 designated initializers (example) make struct literals "named" — the nearest analogue; no naming mechanism at the signature level.

C++: no named arguments; C++20 designated initializers (example) mirror the C analogue; renaming a formal parameter still compiles, but hurts call-site readability.

C#: named arguments (C# 4+, example): the name: syntax is nearly identical to PHP; positional first, named after; renaming a parameter breaks call sites (compile error).

Go: no named arguments — struct literals (example) with field names play the role; signatures are forever positional; the public API's parameter order is the contract.

Java: no named arguments — the Builder pattern (example) self-documents via chained method names; records plus named accessors soften readability; no language-level solution.

JavaScript: no named arguments — the destructured options object idiom (example); defaults hand-written at the destructure; cannot mix with positional arguments.

PHP: the 8.0+ named arguments (example): name: syntax, skip any defaults, self-documenting; positional first then named; passing the same name twice throws Error; renaming a parameter in a child class breaks parent-contract calls (documented).

Python: keyword arguments are native to the language (example): any order, skip defaults; plus **kwargs forwarding and keyword-only parameters (after *).

Rust: no named arguments — struct literals plus Default plus a chained builder (example) carry it; ..Default::default() fills the rest.

TypeScript: the same options object as JS (example) with an interface constraining the shape; defaults live at the destructure site.