Language guide · 5 of 12
Functions & Closures
Functions in Evie are first-class values and can be passed around, returned, and stored.
Function Declarations
fn add(a, b) {
return a + b
}Open in playgroundAnonymous & Arrow Functions
Short single-expression functions can use the arrow => syntax:
// Block syntax
square := fn(x) {
return x * x
}
// Arrow syntax (implicit return)
double := fn(x) => x * 2
// Ideal for higher-order functions:
evens := [1, 2, 3, 4].filter(fn(x) => x % 2 == 0)Open in playgroundVariadic Functions (...args)
Functions can accept a variable number of trailing arguments:
fn log(prefix, ...messages) {
for msg in messages {
io.println(prefix, msg)
}
}
log("[INFO]", "Server started", "Listening on :8080")Open in playgroundClosures
Functions capture bindings from enclosing scopes by reference:
fn createCounter(initial) {
var count := initial
return fn() {
count += 1
return count
}
}
counter := createCounter(10)
counter() // 11
counter() // 12Open in playground