Easy Tutorial
❮ Scala Intro Functions Named Arguments ❯

Scala Functions - Default Parameter Values

Scala Functions

Scala allows you to specify default values for function parameters. With default parameters, you can call a function without passing arguments, and the function will use its default values. If you pass arguments, the passed values will replace the default values. Here is an example:

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

      return sum
   }
}

After executing the above code, the output will be:

$ scalac Test.scala
$ scala Test
Return value: 12

Scala Functions

❮ Scala Intro Functions Named Arguments ❯