Search⌘ K
AI Features

Named Parameter and Default Arguments

Explore how to define and utilize named parameters and default arguments in Kotlin functions. Understand how these features increase code flexibility and readability while preventing common errors by clarifying argument meanings during function calls.

When we declare functions, we often specify optional parameters. A good example is joinToString, which transforms an iterable into a String. We can use it without any arguments, or we can also change its behavior with concrete arguments.

Kotlin 1.5
fun main() {
val list = listOf(1, 2, 3, 4)
println(list.joinToString()) // 1, 2, 3, 4
println(list.joinToString(separator = "-")) // 1-2-3-4
println(list.joinToString(limit = 2)) // 1, 2, ...
}

Default values for parameters

Many more functions in Kotlin use optional parametrization, but how is this ...