A default parameter value lets Kotlin supply an argument automatically when a caller omits it. This reduces overloads, repeated signatures, and uncertainty about omitted arguments. section 3.2.2
Optional formatting arguments
fun <T> joinToString( collection: Collection<T>, separator: String = ", ", prefix: String = "", postfix: String = "" ): String { /* ... */ } joinToString(list) // 1, 2, 3 joinToString(list, "; ") // 1; 2; 3 joinToString(list, postfix = ";", prefix = "# ") // # 1, 2, 3; Regular calls follow declaration order and omit only trailing arguments. Named arguments can omit middle parameters and supply selected arguments in any order. section 3.2.2
Listing 3.2 Declaring joinToString() with default parameter values
fun <T> joinToString(
collection: Collection<T>,
separator: String = ", ", #1
prefix: String = "", #1
postfix: String = "" #1
): String { /* ... */ }
Inspect the complete joinToString declaration with comma, empty-prefix, and empty-postfix defaults. section 3.2.2
Defaults can also be function values. A collection formatter can default transform to { it.toString() }, while callers may omit it, pass a trailing lambda, or replace it with a named argument. section 10.1.4
Listing 10.5 Specifying a default value for a parameter of a function type
fun <T> Collection<T>.joinToString(
separator: String = ", ",
prefix: String = "",
postfix: String = "",
transform: (T) -> String = { it.toString() } #1
): String {
val result = StringBuilder(prefix)
for ((index, element) in this.withIndex()) {
if (index > 0) result.append(separator)
result.append(transform(element)) #2
}
result.append(postfix)
return result.toString()
}
fun main() {
val letters = listOf("Alpha", "Beta")
println(letters.joinToString()) #3
// Alpha, Beta
println(letters.joinToString { it.lowercase() }) #4
// alpha, beta
println(letters.joinToString(separator = "! ", postfix = "! ",
transform = { it.uppercase() })) #5
// ALPHA! BETA!
}
Compare the default transform lambda with calls that use the default conversion, a trailing lambda, or an explicit named transform. section 10.1.4
The default is encoded in the called function rather than copied into each call site. Recompiling the function with a changed default updates callers that omit that argument. Java has no equivalent syntax: Java callers must provide every value unless @JvmOverloads generates overloads for omitted trailing parameters. section 3.2.2



















