Functions
Functions in Harbor are defined with the fn keyword. They
support parameters and implicit return values.
Defining a function
fn add(a, b) {
a + b
}
A function definition starts with fn, followed by the
function name, a parameter list in parentheses, and a body in curly
braces.
Parameters
Parameters are comma-separated identifiers. They are untyped. A function can have zero or more parameters.
fn greet() {
print("Hello!")
}
fn add(a, b) {
a + b
} Return values
Functions do not use a return keyword. The last expression
evaluated in the function body is the return value.
fn double(n) {
n + n
}
result = double(5)
print(result) Output: 10
Calling functions
Call a function by name with arguments in parentheses:
add(1, 2)
print(add(10, 20))
greet() Function calls can be used as expressions — their return value can be assigned to a variable or passed as an argument.
Scope
Parameters are local to the function. Variables assigned inside a function are also local. Functions themselves are stored globally and can be called from anywhere after they're defined.
x = 10
fn example() {
y = 20
x + y
}
print(example()) Output: 30