Named Function Arguments

Suppose the local village has a blacksmith. You might have a function with the following signature, which could be called to forge some new gear for Madrigal:

      fun forgeItem(
          itemName: String,
          material: String,
          encrustWithJewels: Boolean = false,
          quantity: Int = 1
      ): String = ...

One way to call this function is by passing arguments for all four parameters:

    forgeItem("sword", "iron", false, 5)

Another way to make the same function call is like this:

    forgeItem(
        itemName = "sword",
        material = "iron",
        encrustWithJewels = false,
        quantity = 5
    )

This optional syntax uses named function arguments and is an alternative way to provide arguments to a function. In certain cases, it grants several advantages. ...

Get Kotlin Programming: The Big Nerd Ranch Guide, 2nd Edition 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.