Skip to content

Functions

Functions use fun. The return type comes after the parameter list:

fun add(a: Int, b: Int) Int {
return a + b;
}

If no value is returned, the return type can be omitted:

fun logName(name: String) {
// ...
}
var total: Int = add(10, 20);
fun distance(x: Int, y: Int) Int {
return x - y;
}
fun main() Int {
var total: Int = add(4, 5);
return distance(total, 3);
}

For overloads, named arguments, references, and varargs, continue with the next guides.