Scala Default Parameter Values for a Function


Scala lets you specify default values for function parameters. The argument for such a parameter can optionally be omitted from a function call, in which case the corresponding argument will be filled in with the default. Following is an example of specifiying default parameters:

object Test {
   def main(args: Array[String]) {
        println( "Returned Value : " + addInt() );
   def addInt( a:Int=5, b:Int=7 ) : Int = {
      var sum:Int = 0
      sum = a + b

      return sum

When the above code is compiled and executed, it produces the following result:

C:/>scalac Test.scala
C:/>scala Test
Returned Value : 12


If you specify one of the parameters, then first argument will be passed using that parameter and second will be taken from default value.