Skip to content

Functions & Closures

Functions in Chuks are declared with the function keyword and support type annotations:

function add(a: int, b: int): int {
return a + b
}
println(add(3, 4)) // 7

Return types are specified after the parameter list with a colon:

function greet(name: string): string {
return `Hello, ${name}!`;
}
function doWork(): void {
println("Working...");
}

Parameters can have default values, making them optional:

function createUser(name: string, age: int = 25): void {
println(name + " is " + age + " years old");
}
createUser("Alice"); // Alice is 25 years old
createUser("Bob", 30); // Bob is 30 years old

Closures are anonymous functions defined inline. They capture variables from their enclosing scope:

// Lambda syntax
const double = (x: int): int => x * 2;
println(double(5)); // 10
// Multi-line closure
const process = (items: []int): []int => {
var result: []int = [];
for (item of items) {
result.push(item * 2);
}
return result;
}

Functions can accept other functions as arguments and return functions:

function apply(fn: (int) => int, val: int): int {
return fn(val);
}
function multiplier(factor: int): (int) => int {
return (x: int): int => x * factor;
}
const triple = multiplier(3);
println(apply(triple, 10)); // 30

When declaring function type annotations, you can include parameter names for clarity. The syntax is function(name: type, ...): returnType.

// Function type with named parameters
var greet: function(name: string): string = function(name: string): string {
return "Hello, " + name;
}
println(greet("World")); // Hello, World

Named parameters make callback signatures easier to read:

// Named params in callback type
function applyOp(a: int, b: int, op: function(x: int, y: int): int): int {
return op(a, b);
}
var sum = applyOp(10, 5, function(x: int, y: int): int {
return x + y;
});
println(sum); // 15

Function types without names (positional) are also supported:

var double: function(int): int = function(n: int): int {
return n * 2;
}
println(double(21)); // 42

Return function types with named parameters for self-documenting code:

function makeAdder(n: int): function(x: int): int {
return function(x: int): int {
return x + n;
}
}
var add5: any = makeAdder(5);
println(add5(10)); // 15

Functions that perform asynchronous work are declared with async and return a Task<T>:

async function fetchData(url: string): Task<string> {
const resp = await http.get(url);
return resp.body;
}

See the Concurrency guide for details on async/await and spawn.