Named Arguments
In this page:
Named Arguments
Calling a function with paramName = value lets you pass arguments in any order and makes the call site self-documenting, especially useful for functions with many parameters of the same type. Named and positional arguments can be mixed, as long as positional ones come first. This is particularly handy combined with default parameters, letting you skip over earlier defaults.
Note: Named arguments make a call like connect(host = "localhost", port = 8080) far clearer than two unlabeled positional values.
Example: Named Arguments
object Main extends App {
def describe(name: String, age: Int, city: String): Unit = {
println(s"$name is $age years old and lives in $city")
}
describe(name = "Priya", city = "Mumbai", age = 28)
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: