Docs/Language/Functions & Closures
Functions & Closures
One keyword for every function: named, anonymous, generic, async, and comptime.
~4 min read
On this page
Named functions
// The last expression is the return value
fn add(x: i64, y: i64) -> i64 {
x + y
}
// No return value
fn greet(name: str) {
println(f"Hello, {name}!");
}
// Early return
fn clamp_positive(x: i64) -> i64 {
if x < 0 {
return 0;
}
x
}
pub fn main() {
println(add(2, 3)); // 5
greet("Ada"); // Hello, Ada!
println(clamp_positive(-4)); // 0
}A trailing if/else or match is the function’s value as well. In a match that ends a function, each arm is a block:
fn max(a: i64, b: i64) -> i64 {
if a > b { a } else { b }
}
fn name(n: i64) -> str {
match n {
0 => { "zero" }
1 => { "one" }
_ => { "many" }
}
}
fn main() -> i64 {
println(max(3, 9)); // 9
println(name(1)); // one
val s = match 5 { 0 => 10, _ => 20 }; // a match used as a value
println(s); // 20
return 0;
}Parameters can have defaults (fn add(a: i64, b: i64 = 10)), and calls can name their arguments (add(b: 2, a: 1)).
Generic functions
Type arguments are inferred at each call, or given with turbofish f::<T>(...). A bound T: Cap (or T: A + B) is checked at the call:
cap Show {
fn show(self) -> str;
}
struct Money { cents: i64 }
impl Show for Money {
fn show(self) -> str { return f"${self.cents / 100}"; }
}
fn identity<T>(x: T) -> T {
return x;
}
fn twice<T: Show>(v: T) -> str {
return v.show() + " " + v.show();
}
fn main() -> i64 {
println(identity(7)); // 7 (T = i64, inferred)
println(identity::<str>("hi")); // hi (turbofish)
println(twice(Money { cents: 500 })); // $5 $5
return 0;
}Calling twice(5) is rejected:
error: Type error (line 10, in 'main'): i64 does not implement Show (required by T: Show in 'twice').Each generic function is compiled once per concrete type, so there is no run-time dispatch unless you use dyn (Traits & Generics).
Closures
Closures use the same fn keyword:
fn apply(f: fn(i64) -> i64, x: i64) -> i64 {
return f(x);
}
fn main() -> i64 {
val double = fn(x: i64) -> i64 {
x * 2
};
val square = fn(x: i64) -> i64 => x * x; // short form
val k = 3;
val times_k = fn(x: i64) -> i64 => x * k; // captures k
val hello = fn() => println("Hello!");
println(double(4)); // 8
println(square(5)); // 25
println(apply(times_k, 6)); // 18
hello(); // Hello!
return 0;
}A closure captures the variables it names by value, when it is created.
Effects of higher-order functions
A function that takes a function value gets, at each call, the effects of the function passed. apply(inc, 41) is pure, so a !{} function may call it; apply(noisy, 5) has noisy’s IO:
fn apply(f: fn(i64) -> i64, x: i64) -> i64 {
return f(x);
}
fn inc(x: i64) -> i64 { return x + 1; }
fn noisy(x: i64) -> i64 !IO {
println(x);
return x;
}
fn pure_use() -> i64 !{} {
return apply(inc, 41); // OK: inc has no effects
}
fn main() -> i64 {
println(pure_use()); // 42
println(apply(noisy, 5)); // 5, then 5
return 0;
}With apply(noisy, 41) inside pure_use, the program is rejected: function 'pure_use' has inferred effect '!IO' but only declared effects not including it.
Function types
type BinOp = fn(i64, i64) -> i64;
fn sub(a: i64, b: i64) -> i64 { return a - b; }
fn main() -> i64 {
val add: BinOp = fn(a: i64, b: i64) -> i64 => a + b;
var op: BinOp = add;
println(op(5, 3)); // 8
op = sub; // a named function works too
println(op(5, 3)); // 2
return 0;
}async fn and comptime fn are covered in Concurrency and Comptime.