Skip to content

Functions

A function starts with fun, followed by its name and parameters:

fun greet(name: String) {
println("Hello, " + name);
}

Call it by writing its name and arguments:

greet("Ada");

Write the return type after the parameter list:

fun square(value: Int) Int {
return value * value;
}
var result = square(6);

A function returning Void omits the return type:

fun announce(message: String) {
println(message);
}
fun clamp(value: Int, low: Int, high: Int) Int {
if value < low { return low; }
if value > high { return high; }
return value;
}
var safe = clamp(120, 0, 100);

Arguments are evaluated in source order.

Ordinary parameters receive values:

fun excited(name: String) String {
var result = name;
result = result + "!";
return result;
}
var original = "Zap";
var changed = excited(original);
println(original);

Changing the local parameter does not reassign the caller’s variable.

Other parameter contracts have dedicated pages:

Function overloading and argument labels are documented in Overloads and Named arguments.