Skip to content

Latest commit

 

History

History
192 lines (146 loc) · 4.58 KB

functions.md

File metadata and controls

192 lines (146 loc) · 4.58 KB

Functions

← Documentation home · Guide: Control Flow

Functions are declared with func and are first-class values: you can assign them to variables, pass them as arguments, return them, and capture surrounding variables in closures.


Declaration

func add(a, b) {
    return a + b
}

var result = add(10, 20)    // 30

Functions return nil by default. Use return to return a value:

func max(a, b) {
    if (a > b) return a
    return b
}

Default parameter values

A parameter can have a default value, used when the caller leaves the argument out or passes nil:

func greet(name, greeting = "hello", punct = "!") {
    return greeting + " " + name + punct
}

greet("bob")              // "hello bob!"
greet("bob", "hi")        // "hi bob!"
greet("bob", nil, "?")    // "hello bob?"

Parameters with defaults come last: once one parameter has a default, every later one needs one too. A default is evaluated each time it is used, so func collect(x, into = []) gets a new array on every call, and it can refer to earlier parameters (func pad(s, width, fill = " "), func area(w, h = w)).

Calling with fewer arguments than the required parameters, or more than all of them, is an error: greet() reports "expects 1 to 3 arguments but got 0".

Type annotations

Parameters and the return value can be annotated. Annotations are optional and independent — annotate all, some, or none:

// fully annotated
func add(a: int64, b: int64): int64 {
    return a + b
}

// only some parameters
func greet(name: string, times) {
    for (var i = 0; i < times; i++) {
        print("Hello,", name)
    }
}

// return type only
func pi(): float64 {
    return 3.14159
}

// unannotated — inferred
func double(n) {
    return n * 2
}

Accepted type names: int64, uint64, float64, bool, boolean, string, array, table, buffer, function, nil, userdata.

A declared return type is especially useful for recursion. Without it, the compiler cannot know a function's return type while compiling its own body (it hasn't finished compiling yet), so it falls back to generic opcodes. With it, the recursive arithmetic uses fast type-specialized opcodes:

func fibonacci(n: int64): int64 {
    if (n <= 1) return n
    return fibonacci(n - 1) + fibonacci(n - 2)
}

print(fibonacci(10))    // 55

Parameter annotations set the inferred type of each parameter, so operations on them inside the body compile to type-specialized instructions. Because the body trusts the annotation, calls to a named function are checked against it at compile time — a provably wrong-typed argument is a compile error:

func add(a: int64, b: int64): int64 {
    return a + b
}
add("one", 2)
Compile error [script.mob:4]: argument 1 of 'add' is string, but the parameter is annotated int64

nil arguments are always allowed, and arguments whose type the compiler cannot prove (a table field, an untyped parameter passed along) compile normally. Calls through function values — callbacks, functions stored in tables — cannot be checked at compile time and are trusted as annotated.

First-class functions

func apply(f, x) {
    return f(x)
}

func double(n) { return n * 2 }

print(apply(double, 5))    // 10

Anonymous function expressions use the same func keyword without a name — they are common as callbacks to array methods:

var evens = [1, 2, 3, 4, 5]:filter(func(x, i) { return x % 2 == 0; })

Closures

Inner functions capture variables from their enclosing scope:

func make_counter() {
    var count = 0
    func increment() {
        count = count + 1
        return count
    }
    return increment
}

var counter = make_counter()
print(counter())    // 1
print(counter())    // 2

Each call to the outer function creates an independent closure:

var a = make_counter()
var b = make_counter()
a()    // 1
a()    // 2
b()    // 1 — independent from a

Closures nest arbitrarily deep:

func outer(x) {
    func middle(y) {
        func inner(z) {
            return x + y + z
        }
        return inner
    }
    return middle
}

var f = outer(1)(2)(3)    // 6

Concurrency note: a closure passed to spawn may capture upvalues; captured values follow the same rules as arguments — non-shared data is copied (each spawn gets its own), while a captured shared variable is shared by reference. See Concurrency.


Next: Error Handling.