Default Parameters
In this page:
Default Parameters
A parameter can specify a default value in its declaration, def greet(name: String, greeting: String = "Hello"), making it optional at the call site. If the caller doesn't supply it, the default is used. Default parameters are commonly combined with named arguments to skip earlier defaults while still setting a later one.
Note: Default parameters pair naturally with named arguments -- you can override just one default deep in the parameter list without repeating the others.
Example: Default Parameters
object Main extends App {
def greet(name: String, greeting: String = "Hello"): Unit = {
println(s"$greeting, $name!")
}
greet("Alice")
greet("Bob", "Welcome")
}
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: