Scala Functions - Variable Arguments
Scala allows you to indicate that the last parameter of a function can be repeated, meaning we do not need to specify the number of function parameters, and can pass a variable-length argument list to the function.
Scala sets variable arguments (repeatable parameters) by placing an asterisk after the parameter's type. For example:
object Test {
def main(args: Array[String]) {
printStrings("tutorialpro", "Scala", "Python");
}
def printStrings( args:String* ) = {
var i : Int = 0;
for( arg <- args ){
println("Arg value[" + i + "] = " + arg );
i = i + 1;
}
}
}
Executing the above code, the output result is:
$ scalac Test.scala
$ scala Test
Arg value[0] = tutorialpro
Arg value[1] = Scala
Arg value[2] = Python