Skip to main content

Functions & Closures Reference

Functions in Kyna are declared using the fn keyword. Functions are first-class citizens: they can be passed as arguments, assigned to variables, returned from other functions, and stored in collections.


Function Declaration & Signatures​

Parameters are explicitly typed. Return types can be explicitly annotated after a colon : or inferred by the compiler.

# Explicit return type
fn calculateTax(subtotal: float, rate: float): float {
return subtotal * rate;
}

# Inferred return type (inferred as int)
fn add(a: int, b: int) {
return a + b;
}

const totalTax = calculateTax(100.0, 0.08);
print("Total Tax:", totalTax); # 8.0

Void & Non-Returning Functions​

Functions that do not produce a value can omit the return type or specify void. If no return statement is reached, the function implicitly returns null.

fn logMessage(level: str, message: str): void {
print("[" + level + "] " + message);
}

logMessage("INFO", "Server cluster initialized on port 8080");

Default Parameter Values & Optional Arguments​

You can provide default values for trailing parameters:

fn connect(host: str, port: int = 8080, secure: bool = true): str {
const protocol = if (secure) "https://" else "http://";
return protocol + host + ":" + port;
}

print(connect("localhost")); # "https://localhost:8080"
print(connect("localhost", 3000)); # "https://localhost:3000"
print(connect("localhost", 3000, false)); # "http://localhost:3000"

First-Class & Higher-Order Functions​

Functions can be passed directly as arguments to other functions:

fn transform(items: int list, operation: fn(int): int): int list {
var result: int list = [];
loop (var i = 0; i < len(items); i = i + 1) {
result.push(operation(items[i]));
}
return result;
}

fn square(n: int): int {
return n * n;
}

const numbers = [1, 2, 3, 4, 5];
const squared = transform(numbers, square);
print(squared); # [1, 4, 9, 16, 25]

Closures & Lexical Scoping​

Functions can capture variables from their enclosing lexical environment:

fn createCounter(start: int): fn(): int {
var current = start;
return fn(): int {
current = current + 1;
return current;
};
}

const counterA = createCounter(10);
print(counterA()); # 11
print(counterA()); # 12

const counterB = createCounter(100);
print(counterB()); # 101

Anonymous Functions (Lambdas)​

Anonymous functions can be declared inline:

const multiply = fn(x: int, y: int): int {
return x * y;
};

print(multiply(6, 7)); # 42