Skip to content

Function overloads

Functions may share a name when their parameter types differ:

fun describe(value: Int) String {
return "integer";
}
fun describe(value: String) String {
return "text";
}

The compiler selects a matching declaration from the call arguments:

println(describe(42));
println(describe("Zap"));
fun mix(left: Int, right: Float) Int {
return 1;
}
fun mix(left: Float, right: Int) Int {
return 2;
}

Both parameter order and types participate in overload resolution.

Two functions cannot differ only in return type:

// Does not compile:
// fun parse(text: String) Int { return 0; }
// fun parse(text: String) Float { return 0.0; }

The arguments must provide enough information to choose one declaration.

A call is rejected if more than one overload is an equally good match. Prefer overload sets whose parameter types express a clear distinction.

Named arguments can make a call easier to read and may help identify the intended overload.