Skip to main content

Functions

Defining functions​

fn defines a function. The parameters have types after a :, and the return type comes after ->:

fn add(a: i32, b: i32) -> i32 {
a + b
}

add(2, 3);
5

A body in braces is a block. Its value is the value of its last expression, the one without a ; after it. So a + b is what add returns.

When the body is a single expression, write it after = instead, and end the definition with a ;:

fn square(x: i32) -> i32 = x * x;

square(9);
81

Functions can be used before they're defined, and they can call themselves:

fn main_value() -> i32 = fac(5);

fn fac(n: i32) -> i32 = if n == 0 { 1 } else { n * fac(n - 1) };

main_value();
120

Returning early​

return leaves a function right away. A function that doesn't return a value returns (), and its return type can be left out:

fn first_negative(xs: [i32]) -> i32 {
for x in xs {
if x < 0 {
return x;
}
}
0
}

first_negative([3, 1, -4, 1, -5]);
-4

Leaving out types​

The types of parameters and return values are optional. Without them, Bolt works them out from the body:

fn double(x) = x * 2;

double(21);
42

Here Bolt sees that x is multiplied by the integer 2, so x must be an integer too.

A function whose body works for any type becomes generic. This pair works for values of any two types:

fn pair(a, b) = (a, b);

(pair(1, "one"), pair(True, 2.0));
((1, "one"), (True, 2.0))

Types and inference explains how this works and when you'd want to write types anyway.

Keyword arguments​

Not implemented yet

Keyword arguments are planned, but Bolt doesn't support them yet. A call like register(name="Davis") is read as an assignment to a variable name, so it fails with "cannot find name".

An argument can be passed by the name of its parameter, like in Python. This makes calls with many arguments easier to read:

fn register(name: String, email: String) -> String = name <> " <" <> email <> ">";

// These calls are the same.
(
register("Davis", "davis@example.com"),
register(name="Davis", email="davis@example.com"),
);
("Davis <davis@example.com>", "Davis <davis@example.com>")

Default values of parameters​

Not implemented yet

Default values are planned, but Bolt doesn't support them yet. A function with a default value can be defined, but a call that leaves the argument out fails with "expected 2 arguments, found 1".

A parameter can have a default value, written after =. A call can then leave that argument out:

fn greet(name: String, greeting: String = "Hello") -> String = greeting <> ", " <> name <> "!";

(greet("Sam"), greet("Sam", "Welcome"));
("Hello, Sam!", "Welcome, Sam!")

With keyword arguments, a call can give only the defaults it wants to change:

fn shoot(x: f64, y: f64, strength: f64 = 1.0, spin: f64 = 0.0) -> (f64, f64) =
(x * strength, y * strength + spin);

(shoot(1.0, -1.0), shoot(1.0, -1.0, spin=0.5));
((1.0, -1.0), (1.0, -0.5))

Functions are values​

A function can be passed to another function, stored in a variable, and returned:

fn twice(f, x) = f(f(x));
fn inc(n: i32) -> i32 = n + 1;

let g = inc;
twice(g, 10);
12

Closures​

A closure is a function without a name, written between | bars. Its body is a single expression, or a block:

let inc = |x| x + 1;
let add = |a, b| a + b;
let clamp = |x: i32| -> i32 {
if x > 100 { 100 } else { x }
};

(inc(1), add(2, 3), clamp(250));
(2, 5, 100)

fn (params) = body is another way to write one, if you prefer the look of a function:

let mul = fn (a, b) = a * b;
mul(6, 7);
42

A closure can use the variables around it, and change them when they're mut:

let mut count = 0;
let tick = || { count += 1; };
tick();
tick();
tick();
count;
3

A closure that can change what it uses, like tick, is a mut fn: see Mutability.

Closures make functions that build functions easy:

fn compose(f, g) = |x| f(g(x));
fn adder(n) = |x| x + n;

let add_then_double = compose(|x| x * 2, adder(1));
add_then_double(5);
12

Method call syntax​

x.f(y) is another way to write f(x, y). It calls the function f that's in scope, with x as its first argument. That lets you chain calls from left to right:

fn bump(n: i32, by: i32) -> i32 = n + by;
fn halve(n: i32) -> i32 = n / 2;

10.bump(4).halve();
7

Unlike in many languages, functions don't belong to a type: any function in scope can be called this way. A function in the namespace of a type, like Point.at (see Functions of a type), isn't in scope by its name alone, so it's called by its path.

Functions with the same name​

Several functions can have the same name, as long as they take different numbers or types of arguments. A call uses the one that its arguments fit:

fn describe(n: i32) -> String = "a number";
fn describe(b: Bool) -> String = "a truth value";
fn describe(n: i32, unit: String) -> String = "a number of " <> unit;

(describe(3), describe(True), describe(3, "apples"));
("a number", "a truth value", "a number of apples")

All the arguments count, not just the first, and so does whether an array or struct is mut. The functions can come from different files, like your own file and an import, or the prelude: a len for your own type works next to the prelude's len for a Vec.

The types of the arguments must leave one function. When they don't, the call is an error, rather than a guess:

fn describe(n: i32) -> String = "a number";
fn describe(b: Bool) -> String = "a truth value";

fn show_it(x) = describe(x);
[E0237] Error: more than one function `describe` fits this use: `fn(i32) -> String` and `fn(Bool) -> String`; give the arguments types that leave one

Writing x: i32 fixes it. A function that works for any type that has a describe is what classes are for.

For the same reason, two functions with the same name that one call could fit both are an error where the second is defined, even if they give different types:

fn describe(n: i32) -> String = "a number";
fn describe(m: i32) -> String = "an integer";
[E0238] Error: this `describe`, of type `fn(i32) -> String`, overlaps the `describe` of type `fn(i32) -> String`: a call could fit both, so it couldn't choose one; give them different numbers or types of parameters

A method of a class fits any type with an impl, so a function with its name must take another number of arguments.

Custom operators​

You can define your own binary operators, made of symbols like <> or <$>. The definition looks like the operator in use:

fn a <> b = (a, b);

1 <> 2 <> 3;
((1, 2), 3)

Custom operators bind tighter than the ones of the language, like +, and group from left to right. Those are functions of the prelude too, like fn a + b = add(a, b); to make + work on a type of your own, give it an impl of the operator's class, as in classes.

Next: control flow.