Named Parameter and Default Arguments
Understand function parameter flexibility and clarity with named arguments.
We'll cover the following...
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.
Press + to interact
fun main() {val list = listOf(1, 2, 3, 4)println(list.joinToString()) // 1, 2, 3, 4println(list.joinToString(separator = "-")) // 1-2-3-4println(list.joinToString(limit = 2)) // 1, 2, ...}
Default values for parameters
Many more functions in Kotlin use optional parametrization, ...