Skip to content

Latest commit

 

History

History
34 lines (25 loc) · 1001 Bytes

named-arguments.md

File metadata and controls

34 lines (25 loc) · 1001 Bytes
layout title discourse partof num next-page previous-page prerequisite-knowledge redirect_from
tour
Named Arguments
true
scala-tour
34
packages-and-imports
default-parameter-values
function-syntax
/tutorials/tour/named-arguments.html

When calling methods, you can label the arguments with their parameter names like so:

def printName(first: String, last: String): Unit = {
  println(first + " " + last)
}

printName("John", "Smith")  // Prints "John Smith"
printName(first = "John", last = "Smith")  // Prints "John Smith"
printName(last = "Smith", first = "John")  // Prints "John Smith"

Notice how the order of named arguments can be rearranged. However, if some arguments are named and others are not, the unnamed arguments must come first and in the order of their parameters in the method signature.

printName(last = "Smith", "john") // error: positional after named argument

Note that named arguments do not work with calls to Java methods.