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

Scala Functions – Variable Arguments

Scala Functions - Variable Arguments

Scala functions Scala Functions

Scala lets you specify that the last parameter of a function can be repeated, meaning you do not need to fix the number of arguments and can pass a variable-length argument list.

Scala sets a variadic (repeatable) parameter by placing an asterisk after the parameter's type. For example:



object Test {

   def main(args: Array[String]) {

        printStrings("Runoob", "Scala", "Python");

   }

   def printStrings( args:String* ) = {

      var i : Int = 0;

      for( arg <- args ){

         println("Arg value[" + i + "] = " + arg );

         i = i + 1;

      }

   }

}

The output of the code above is:



$ scalac Test.scala

$ scala Test

Arg value[0] = Runoob

Arg value[1] = Scala

Arg value[2] = Python

Scala functions Scala Functions