Scala function-default parameter value


Release date:2023-11-16 Update date:2023-11-16 Editor:admin View counts:148

Label:

Scala function-default parameter value

Scala can specify the default parameter value for the function parameter, using the default parameter, you do not need to pass parameters in the process of calling the function, then the function will call its default parameter value, if the parameter is passed, the transfer value will replacethe default value. Examples are as follows:

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
   }
}

Execute the above code, and the output is as follows:

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

Powered by TorCMS (https://github.com/bukun/TorCMS).