Scala provides the ability to give parameters default values that can be used to allow a caller to omit those parameters.
package com.scala.test
object TestDefault {
def dummy (a:String, b:String=”default”,c:Int=1) = {
println(a+b+c)
}
def main(args: Array[String]) {
dummy(“test”,”named”,2)
dummy(“test”) // We have not passed the default parameters b and c
dummy(“test”,”again”) // We have not passed the default parameter c
dummy(“test”,c=5) // We have not passed the default parameter b
}
}
Output
testnamed2
testdefault1
testagain1
testdefault5
Default Parameters have the following Advantages:
- It is possible to implement Optional Parameters.
- It avoids code duplication.
Notes:
#1. default parameters in Scala are not optional when called from Java code