Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Scala Functions – Default Parameter Values

Scala Functions - Default Parameter Values

Scala functions Scala Functions

Scala lets you specify default values for function parameters. With default parameters, you can omit an argument when calling the function, and the function uses the default value; if you pass an argument, the passed value replaces the default. Example:



object Test {

   def main(args: Array[String]) {

        println( "返回值 : " + addInt() );

   }

   def addInt( a:Int=5, b:Int=7 ) : Int = {

      var sum:Int = 0

      sum = a + b



      return sum

   }

}

The output of the code above is:



$ scalac Test.scala

$ scala Test

返回值 : 12

Scala functions Scala Functions