Skip to content

Functions

NativeBrowserNode1 min read

A function gives a reusable task a name. Declare it with fun, describe the values it accepts, and give its return type when it produces a result.

At a glance

fun add(a: int, b: int): int {
    return a + b;
}

let result = add(3, 4);
  • Declare a function with fun, typed parameters, and a return type.
  • Leave out the return type when the function produces no value.

Explore this topic

Defining and calling

fun add(a: int, b: int): int {
    return a + b;
}

let result = add(3, 4);
  • fun, then the name.
  • Parameters are name: type, comma-separated.
  • : ReturnType follows the parameter list.

The return type is optional for functions that return nothing, so these are equivalent:

fun greet() {
    System.println("hi");
}

fun greet(): void {
    System.println("hi");
}

Returning a value

Use return. Dream checks that every path returns when the return type is not void:

fun clamp(value: int, lo: int, hi: int): int {
    if value < lo {
        return lo;
    }
    if value > hi {
        return hi;
    }
    return value;
}

In a void function a bare return; exits early:

fun log_positive(n: int): void {
    if n < 0 {
        return;
    }
    System.println(n);
}

Functions can call themselves — recursion works as expected:

fun fib(n: int): int {
    if n <= 1 {
        return n;
    }
    return fib(n - 1) + fib(n - 2);
}

Public functions and entry point

Functions are file-private by default. Mark one public to import it from other files and export it to the WebAssembly host (see Imports). A public function cannot expose a non-public class:

// public fun make(): Secret { ... }   // error if Secret is not public
public fun compute(n: int): int {
    return n * n;
}

The runtime starts a program by calling main. Every runnable program needs one; its return type can be omitted:

fun main() {
    System.println("hello");
}