Default Arguments in Kotlin

Level 3119 keystrokes

Defines a function with a default parameter value and calls it with and without that argument.

Kotlin code

// greeting = "Hello" sets a default used when the argument is omitted
1fun greet(name: String, greeting: String = "Hello") {
// Print the greeting and the name
2 println("$greeting, $name!")
3}
4
// greeting is omitted, so "Hello" is used: Hello, Alice!
5greet("Alice")
// Passing "Hi" overrides the default: Hi, Bob!
6greet("Bob", "Hi")

Output

Hello, Alice!
Hi, Bob!
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.

  • "× 10
  • (× 4
  • )× 4
  • ,× 3
  • :× 2
  • $× 2
  • !× 1
  • {× 1
  • }× 1
  • =× 1

More Kotlin examples at level 3

← Kotlin typing practice