Home »
Scala language
Repeated Method Parameters in Scala
Scala Repeated Method Parameters: It is the concept of accepting multiple parameters by a method. In this tutorial, we will learn about repeated method parameter in Scala with examples.
Submitted by Shivang Yadav, on September 21, 2019
Scala Repeated Method Parameters
The repeated parameter is the concept of defining parameters in which parameters of the same data type can be passed to a method n number of times, where n belongs to 0 to infinity i.e. method can accept any parameters.
Scala programming language supports repeated method parameter, which is of great help when the number of parameters passed is not defined at compile time. Using repeated method parameter, the program can accept an unlimited number of parameters.
Properties of repeated method parameter
- There can be only one repeated parameter allowed in a method.
- The data type of all repeated parameters is the same and is defined at once with the same variable name.
- The repeated method parameter should always be the last defined parameter of the method.
Example 1:
This program displays the property of the program that repeated method parameter should be only single and data.
object MyClass {
def adder(x:Int*){
var sum = x.fold(0)(_+_)
printf("The sum is " + sum)
}
def main(args: Array[String]) {
adder(1,5,6,67,8,2)
}
}
Output
The sum is 89
Example 2:
Property displayed in the program is that if there are multiple parameters passed to a method, then the last one should be repeated parameter passed to the method.
object MyClass {
def salary(str:String , x:Int*){
var sal = x.product
printf("The salary of the intern at " + str + " is " + sal)
}
def main(args: Array[String]) {
salary("Include Help",90, 100)
}
}
Output
The salary of the intern at Include Help is 9000