Closures and anonymous functions
Demonstrates closures capturing outer variables; Java expresses them with lambdas, Rust through its three Fn capabilities.
C C23
#include <stdio.h>
// C has no anonymous functions or closures; a "function pointer + context struct" emulates capturing outer variables
typedef struct {
int factor; // captured context (equivalent to PHP use ($factor), by value)
} ScaleCtx;
int scale(int x, const ScaleCtx *ctx) {
return x * ctx->factor;
}
int main(void) {
int factor = 10;
ScaleCtx ctx = { factor };
// PHP's $scale and $arrow are both just function pointer + context in C, with no syntactic difference
printf("%d\n", scale(3, &ctx));
printf("%d", scale(3, &ctx));
return 0;
}
30 30
C++ C++26
#include <functional>
#include <iostream>
// lambda closure captures the outer variable n (by value)
std::function<int(int)> makeAdder(int n) {
return [n](int x) { return x + n; };
}
int main() {
auto add5 = makeAdder(5);
std::cout << add5(1) << "\n";
auto square = [](int x) { return x * x; }; // anonymous lambda
std::cout << square(7) << "\n";
return 0;
}
6 49
C# v11.0.100
Func<int, int> MakeAdder(int n) => x => x + n; // lambda closure: captures the outer variable n
var add5 = MakeAdder(5);
Console.WriteLine(add5(1));
var square = (int x) => x * x; // anonymous lambda
Console.WriteLine(square(7));
6 49
Go v1.27.4
package main
import "fmt"
// the function returns an anonymous function: the closure captures the outer variable n
func makeAdder(n int) func(int) int {
return func(x int) int {
return x + n
}
}
func main() {
add5 := makeAdder(5)
add10 := makeAdder(10)
fmt.Println(add5(1))
fmt.Println(add10(1))
}
6 11
Java v27.0.1
import java.util.function.Function;
public class Main {
static Function<Integer, Integer> makeAdder(int n) {
return x -> x + n; // lambda captures n (must be effectively final)
}
public static void main(String[] args) {
Function<Integer, Integer> add5 = makeAdder(5);
System.out.println(add5.apply(1));
Function<Integer, Integer> square = x -> x * x; // anonymous lambda
System.out.println(square.apply(7));
}
}
6 49
JavaScript v26.13.1
function makeAdder(n) {
return (x) => x + n; // arrow-function closure: lexically captures n
}
const add5 = makeAdder(5);
console.log(add5(1));
const square = (x) => x * x; // anonymous arrow function
console.log(square(7));
6 49
PHP v8.6.0
$factor = 10;
// anonymous function: outer variables must be captured manually with use (by value)
$scale = function (int $x) use ($factor): int {
return $x * $factor;
};
// arrow function fn: automatically captures the used outer variables by value
$arrow = fn(int $x): int => $x * $factor;
echo $scale(3), "\n";
echo $arrow(3);
30 30
Python v3.15.2
def make_adder(n):
def adder(x): # a nested function is naturally a closure: it captures the outer variable n
return x + n
return adder
add5 = make_adder(5)
add10 = make_adder(10)
print(add5(1))
print(add10(1))
print((lambda x: x * x)(7)) # lambda: single-expression anonymous function
6 11 49
Rust v1.99.1
fn main() {
let factor = 10;
let scale = |x| x * factor; // the closure captures factor automatically
println!("{}", scale(3));
let mut count = 0;
let mut bump = || { // mutable capture (corresponds to FnMut)
count += 1;
count
};
bump();
println!("{}", bump());
// a closure automatically implements one of Fn / FnMut / FnOnce depending on how it captures
}
30 2
TypeScript v26.13.1
function makeAdder(n: number): (x: number) => number {
return (x) => x + n; // arrow-function closure: lexically captures n
}
const add5 = makeAdder(5);
console.log(add5(1));
const square = (x: number) => x * x; // anonymous arrow function
console.log(square(7));
6 49
Language differences
C: no closure syntax — a function pointer plus a context struct emulates by-value capture.
C++: a lambda is passed once carried by std::function or auto.
Go: function literals can capture outer variables.
Java: lambdas can only capture effectively final variables and are carried by a functional interface type (Function<T,R>, etc.).
JavaScript: arrow functions capture lexically (mind the this difference between regular and arrow functions).
PHP: anonymous functions capture manually with use — by value by default, use (&$v) by reference; arrow functions fn capture outer variables by value automatically.
Python: inner functions are natural closures; lambda is limited to a single expression.
Rust: closures are written |args| expression and automatically implement one of the three capabilities Fn/FnMut/FnOnce depending on how they capture.