Skip to content

Functions

Functions in Nubo can be declared by name or created inline as values.

Use the fn keyword to declare a function:

fn add(a: int, b: int) int {
return a + b
}
println(add(2, 3))

Use void when a function does not return a value:

fn say(message: string) void {
println(message)
}
say("Hello")

Anonymous functions can be assigned to variables or passed as arguments:

let add = fn(a: int, b: int) int {
return a + b
}
println(add(1, 2))

They are especially useful for callbacks:

let total: int = 0
bytes("hello").map(int).each(fn(n: int) {
total = total + n
})
println(total)

Use return to return a value from a function:

type hasLen: iface {
length() int
}
fn length(obj: hasLen) int {
return obj.length()
}

Parameters are written with a name and type:

fn greet(name: string) void {
println("Hello", name)
}

A function can accept any value matching an inline interface:

type hasLen: iface {
length() int
}
fn len(obj: hasLen) int {
return obj.length()
}
const name: string = "John"
println(len(name))