Functions
Defining functions, return values, and effect annotations.
Functions
Functions in Sounio are expressions: the last expression in a block is the return value.
Basic Functions
fn add(a: i32, b: i32) -> i32 {
a + b
}
fn greet(name: string) {
print("Hello, ")
println(name)
}
Function Calls
let sum = add(3, 5) // 8
greet("World") // Hello, World!
Effects Declaration
The spec requires that side effects are declared via with ... in the function signature.
fn log(msg: string) with IO {
println(msg)
}
Multiple effects can be combined:
fn safe_div(a: i32, b: i32) -> i32 with Panic {
if b == 0 {
panic("division by zero")
}
a / b
}
Methods (impl)
Functions can be defined on types:
struct Point {
x: f64,
y: f64,
}
impl Point {
fn new(x: f64, y: f64) -> Point {
Point { x: x, y: y }
}
fn distance(&self, other: &Point) -> f64 {
let dx = self.x - other.x
let dy = self.y - other.y
sqrt(dx * dx + dy * dy)
}
}
Spec note: generics, closures, and higher-order functions are supported by the design, but some combinations may still be evolving in the compiler.