Named parameters

Function parameters have names that we use to access the argument data when passed. In Kotlin, we can make use of parameter names at the function call site to control which parameter an argument value is assigned to.

Let's revisit our previous examples to better understand how to use named parameters. We'll use our helloFunctions implementation that has two parameters:

fun helloFunctions(greeting: String, name: String) { ... }

We see the two parameters, greeting and name, both of the String type. If we want to use this function, it might look something like this:

fun main(args: Array<String>) {    helloFunctions("Hello", "Kotlin")}

This is very straightforward, but because both parameters are the same type, there's always the ...

Get Mastering Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.