Arguments
Explore how default and named arguments in Kotlin allow you to write more concise and readable functions. This lesson helps you understand setting default parameter values, using named arguments for clarity, and combining these features to minimize code duplication and improve maintainability.
Default arguments
Kotlin allows us to provide function parameters with a default value, which is used if the corresponding argument is omitted on the call.
fun String.splitString(separator: Char = ',')
When splitting a string with the extension function above (more on extension functions later), we can choose any separator. However, thanks to the default value “,” we can also omit it, if the “,” is actually what we need:
str.split('|') // splits str on ...