Skip to main content

Functions

Typed reusable behaviour and program entry points.

Declaring and calling functions

functions.sar
func add(left int32, right int32) int32 {
    return left + right
}

func show_total(value int32) {
    print("Total: " + value)
}

func main() {
    local var total int32 = add(10, 20)
    show_total(total)
}

A function begins with func, followed by its name and parameter list. Every parameter has a name followed by a type. Parameters are separated by commas. Put the return type after the closing parenthesis, or omit it for a function that returns no value.

Calls use positional arguments and must provide exactly the declared number. Numeric values are converted to the parameter type automatically. Managed arguments share their value with the function rather than copying it.

Return values

return expression immediately leaves a value-returning function. Every possible path through such a function must return a value of its declared type. Functions without a return type do not use a return statement.

The main function

A native executable requires exactly func main(), with no parameters and no return type. Execution begins there.