Named Arguments

fun format(userName: String, domain: String) = "$userName@$domain"

fun main(args: Array<String>) {
    println(format("mario", ""))                         // 1
    println(format(userName = "foo", domain = ""))           // 2
    println(format(domain = "", userName = "pepe"))         // 3
  1. As in Java, Kotlin supports passing arguments to methods and constructors according to their order of definition.
  2. Kotlin also supports named arguments to allow clearer invocations and avoid mistakes that otherwise would not be detected by the compiler (e.g. switching the correct order of the arguments and constructing an email in the form
  3. When invoking a method or constructor, named arguments do not have to respect the original order of definition.