TExcelTAD
Home › Kotlin › Kotlin Default & Named Arguments

Kotlin Default and Named Arguments

In Java you often write several overloaded versions of the same method just to make some parameters optional. Kotlin solves this with default arguments and makes calls self-documenting with named arguments.

Default arguments

Give a parameter a default value with =. Callers may then leave it out:

kotlin · try it yourself
fun brew(drink: String, size: String = "medium", sugar: Int = 0) {
    println("$size $drink with $sugar sugar(s)")
}

fun main() {
    brew("tea")
    brew("coffee", "large")
    brew("cocoa", "small", 2)
}
medium tea with 0 sugar(s)
large coffee with 0 sugar(s)
small cocoa with 2 sugar(s)

One function replaces what would have been three overloads in Java.

Named arguments

Write name = value in the call to say which parameter you're passing. This lets you skip earlier defaults and makes calls with many parameters readable:

kotlin · try it yourself
fun brew(drink: String, size: String = "medium", sugar: Int = 0) {
    println("$size $drink with $sugar sugar(s)")
}

fun main() {
    brew("espresso", sugar = 1)                   // skip size
    brew(sugar = 3, drink = "chai", size = "large") // any order
}
medium espresso with 1 sugar(s)
large chai with 3 sugar(s)

Compare these two calls — which one can you understand without looking up the function?

kotlin
createUser("ana", true, false, 30)
createUser("ana", isAdmin = true, sendEmail = false, sessionMinutes = 30)

Tip: Use named arguments for Boolean flags and for any call with several parameters of the same type. They prevent mix-ups that the compiler can't catch.

Mixing positional and named arguments

Positional arguments normally come first. Once you name an argument out of order, the following arguments must be named too:

kotlin · try it yourself
fun box(width: Int, height: Int, depth: Int = 1) = width * height * depth

fun main() {
    println(box(2, 3))
    println(box(2, height = 3, depth = 4))
    println(box(depth = 5, width = 1, height = 2))
}
6
24
10

Defaults can use other parameters

A default value can be any expression, even one that refers to earlier parameters:

kotlin · try it yourself
fun formatName(first: String, last: String, display: String = "$first $last") = display

fun rectangle(width: Int, height: Int = width) = "$width x $height"

fun main() {
    println(formatName("Ada", "Lovelace"))
    println(formatName("Ada", "Lovelace", display = "Countess"))
    println(rectangle(4))
    println(rectangle(4, 2))
}
Ada Lovelace
Countess
4 x 4
4 x 2

Errors you might see

Passing a value of the wrong type, or forgetting a required parameter, is a compile error:

kotlin · try it yourself
fun brew(drink: String, size: String = "medium") = "$size $drink"

fun main() {
    println(brew(size = "small"))
}
File.kt:4:13: error: No value passed for parameter 'drink'.

Defaults vs. overloads

Overloads Default arguments
Lines of code one function per combination one function
Readable calls only by position names available
Java callers work as usual need @JvmOverloads to see the shorter versions

Note: Named arguments also work with constructors — you'll use them a lot with classes and data classes: User(name = "Ana", age = 29).

Test yourself

Given fun f(a: Int, b: Int = 2) = a * b, what is f(5)?

  1. 5
  2. 7
  3. 10
  4. Compile error

What do named arguments allow you to do?

  1. Rename a function
  2. Pass arguments in any order and skip defaulted parameters
  3. Make parameters mutable
  4. Return several values

Can a default value refer to an earlier parameter, like fun r(w: Int, h: Int = w)?

  1. Yes
  2. No
  3. Only for constants
  4. Only in classes