Named arguments in Kotlin

Level 3195 keystrokes

With named arguments (name = value) each value is matched to its parameter by name, so calls are easier to read and the order can be changed freely.

Kotlin code

// A function with three parameters of different types
1fun createUser(name: String, age: Int, active: Boolean) {
// Print the three values separated by commas
2 println("$name, $age, $active")
3}
4
// Named arguments in declaration order: prints Alice, 25, true
5createUser(name = "Alice", age = 25, active = true)
// Order changed, but names map each value correctly: prints Bob, 30, false
6createUser(active = false, name = "Bob", age = 30)

Output

Alice, 25, true
Bob, 30, false
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Kotlin code and how often they appear.

  • ,× 8
  • "× 6
  • =× 6
  • (× 4
  • )× 4
  • :× 3
  • $× 3
  • {× 1
  • }× 1

More Kotlin examples at level 3

← Kotlin typing practice