Latest web development tutorials

Scala function - the variable parameters

Scala function Scala function

Scala allows you to specify the function of the last parameter can be duplicated, that is, we do not need to specify the number of function arguments can be passed to a function variable-length argument list.

Scala after by the type parameter set to put an asterisk variable parameters (repeatable parameters). E.g:

object Test {
   def main(args: Array[String]) {
        printStrings("w3big", "Scala", "Python");
   }
   def printStrings( args:String* ) = {
      var i : Int = 0;
      for( arg <- args ){
         println("Arg value[" + i + "] = " + arg );
         i = i + 1;
      }
   }
}

Implementation of the above code, the output is:

$ scalac Test.scala
$ scala Test
Arg value[0] = w3big
Arg value[1] = Scala
Arg value[2] = Python

Scala function Scala function