Skip to content

Functions

A function is a named block of code with parameters and a return type.

Syntax

func <return-type> <name>[<params>]
    <body>
end

Parameters are written as <type> <name>, separated by commas:

func i64 add[i64 a, i64 b]
    a b + return
end

Return type

The return type follows func:

func void f[]        // returns nothing
func int f[]         // returns int
func *char[] f[]     // returns a C-string pointer

A void function cannot return a value:

func void f[]
    32 return    // error: void function returns a value
end

A non-void function must return a value:

func int f[]
    // error: missing return
end

Calling

A function is called by pushing its arguments onto the stack and then calling it:

2 3 add

call is optional

call f and f are equivalent for direct calls. The call keyword is required only when calling through a function pointer variable or using modules:

call v      // v is a function pointer variable
call mod.v  // v is a member of `mod` module

Parameters

Arguments are passed on the stack, in order. The function pops them according to its signature into parameter variables.

func i64 add[i64 a, i64 b]
    a b + return
end

2 3 add    // 5

void functions

A void function produces no value:

func void print_hello[]
    "hello" print
end

call print_hello

extern functions

An extern function is declared but not defined in the current compilation unit. It is resolved at link time:

extern func int puts[*char[]]

extern functions have no body.

See extern for details.

pub functions

A pub function is exported from the module and emitted as a public symbol:

pub func f[]
    // ...
end

See pub for details.

Recursion

Functions may call themselves:

func i64 fact[i64 n]
    n 1 <= if
        1 return
    end
    n n 1 - fact * return
end