Skip to content

Functions

Functions

Basic declaration

Functions are declared with func, followed by the name, parameters in parentheses, an optional return type after ->, and : to open the body:

func add(a: i32, b: i32) -> i32:
return a + b
func main() -> i32:
let result = add(10, 32)
return result - 42

Parameters always require explicit type annotations. The return type is optional — if omitted it is inferred from return statements.

Inferred return types

func double(value: i32):
return value * 2

The compiler infers the return type as i32. A function with no return value infers void.

Entry point

func main() -> i32 is the standard entry point. There are no top-level executable statements — all logic lives inside functions:

func main() -> i32:
println("Program started")
return 0

Calling functions

func square(n: i32) -> i32:
return n * n
func sum_of_squares(a: i32, b: i32) -> i32:
return square(a) + square(b)
func main() -> i32:
return sum_of_squares(3, 4) - 25

Recursion

func fib(n: i32) -> i32:
if (n < 2):
return n
return fib(n - 1) + fib(n - 2)
func main() -> i32:
return fib(10) - 55

Generic functions

Functions can take type parameters with built-in constraints:

func abs<T: Numeric>(value: T) -> T:
if (value < 0):
return -value
return value
func main() -> i32:
let x: i32 = abs(-5)
return x - 5

Supported constraints: Ordered, Eq, Numeric.

extern func

Use extern func to declare external C functions (see FFI & Import):

extern func printf(fmt: str) -> i32
func main() -> i32:
printf("Hello, Thagore!\n")
return 0