Home »
Scala
Function with variable arguments in Scala
By IncludeHelp Last updated : October 20, 2024
Function with variable arguments
A function takes some variables in its definition as arguments that are passed when it is called. The function uses these arguments to calculate some results for which the function is used. Sometimes there are cases when we do not know how many arguments will be passed to the function in the runtime?
For these conditions, one approach can be redefining the same function ( overloading the function ) for every case. But this method is not that efficient. So, for this case programming languages define varargs that are used to take variable arguments in the function call.
In Scala, also variable arguments are used. In Scala, use have to indicate that the last argument can be repeated i.e. it can be any variable. The program treats this as an array of that datatype and expects the user to input multiple arguments which can be treated as an array. To show the compiler that this function is going to accept multiple arguments * is used. This asterisk symbol symbolizes that the argument is an array of the specified data type.
For example, Strings* symbolizes an array of Strings.
Syntax
def functionName(args: String*){
//code to be executed
}
Explanation
The preceding Syntax initializes a function functionname(), this function uses variable arguments of the datatype string in its definition.
Example of function with variable arguments
object myClass {
def printStrings( arguments:String* ) = {
var i : Int = 0;
println("The array of arguments recieved is : ")
for( string <- arguments ){
println("Arg value[" + i + "] = " + string );
i = i + 1;
}
}
def main(args: Array[String]) {
println("This program demonstrate use of variable arguments. ")
println("first Run :")
printStrings("Includehelp", "is", "awesome");
println("Second Run :")
printStrings("I", "Love", "programming", "in", "scala");
}
}
Output
This program demonstrate use of variable arguments.
first Run :
The array of arguments recieved is :
Arg value[0] = Includehelp
Arg value[1] = is
Arg value[2] = awesome
Second Run :
The array of arguments recieved is :
Arg value[0] = I
Arg value[1] = Love
Arg value[2] = programming
Arg value[3] = in
Arg value[4] = scala
Code explanation
The preceding code uses a function printstrings() that takes variable arguments (varargs). And prints each of their values in separate lines. In the main function, The code has called the function printstrings() two times. The first time with arguments " 'includehelp', 'is', 'awesome' " it takes three arguments in the array this time. The second time the function is called with arguments " I love programming in Scala" this time the number of arguments is 5. In both cases, the program executes and runs properly.
The main() function
The main() function in Scala also uses varargs to take multiple command line arguments. Now, as we are familiar with the functioning of variable arguments, we can use command line arguments in the main function to give input to our code.