Skip to content

Functions

Functions are the primary building blocks of Stride programs. They are declared using the fn keyword.

Declaration and Invocation

A function declaration consists of the fn keyword, followed by the function name, a parenthesized list of parameters, and a return type after a colon.

stride
// Function declaration
fn add(a: i32, b: i32): i32 {
    return a + b;
}

// Function with no return value (void)
fn greet(name: string): void {
    printf("Hello, %s!\n", name);
}

fn main(): void {
    // Invoking functions
    const result: i32 = add(10, 5);
    printf("10 + 5 = %d\n", result);
    
    greet("Stride");
}

Function Parameters

Parameters are passed by value by default. Each parameter must have a name and a type.

Return Values

Functions return values using the return keyword. If a function is declared with a void return type, it does not need a return statement, or it can use return without a value.

Function Types

Functions in Stride are first-class citizens and have types. A function type is denoted by its parameter types and its return type: (param1_type, param2_type) -> return_type.

stride
fn add(a: i32, b: i32): i32 {
    return a + b;
}

fn main(): void {
    const op: (i32, i32) -> i32 = add;
    const result: i32 = op(10, 20);
}

Lambdas (Anonymous Functions)

Lambdas allow you to define functions inline. They follow the syntax (params): return_type -> { body }.

stride
fn main(): void {
    const multiply: (i32, i32) -> i32 = (x: i32, y: i32): i32 -> {
        return x * y;
    };
    
    const result: i32 = multiply(5, 10);
}

Lambdas can also capture variables from their outer scope:

stride
fn main(): void {
    const factor: i32 = 10;
    const multiply_by_factor: (i32) -> i32 = (x: i32): i32 -> {
        return x * factor;
    };
    
    printf("Result: %d\n", multiply_by_factor(5)); // Outputs 50
}

The main Function

The main function is the entry point of every Stride program. It should have a void return type.

stride
fn main(): void {
    // Your code starts here
}

External Functions

You can also declare externally linked functions (e.g., from C libraries) using the extern keyword. These declarations do not have a body and must end with a semicolon.

stride
// Declare printf from the C standard library
extern fn printf(format: string, ...): i32;

// Declare other external functions
extern fn system_time_ns(): u64;
extern fn exit(code: i32): void;

fn main(): void {
    printf("System time: %llu ns\n", system_time_ns());
}

Released under the MIT License.