Scala | Repeated Method Parameters Last Updated : 29 Mar, 2019 Comments Improve Suggest changes Like Article Like Report In Scala, Repeated Method parameters are supported, which is helpful when we don't know the number of arguments a method requires. This property of Scala is utilized in passing limitless parameters to a method defined. Important points : The methods with Repeated Parameters should have each of the parameters of same type. A Repeated parameter is always the last parameter of the method defined. The method defined by us, can have only one parameter as Repeated Parameters. Example: Scala // Scala program of repeated // parameters // Creating object object repeated { // Main method def main(args: Array[String]) { // Creating a method with // repeated parameters def add(x: Int*) : Int = { // Applying 'fold' method to // perform binary operation x.fold(0)(_+_) } // Displays Addition println(add(2, 3, 5, 9, 6, 10, 11, 12)) } } Output: 58 In order to add any number of extra parameters, we need to put * mark after the type of the parameter being used. Some more examples of Repeated Parameters: An Array can be passed in the Repeated Parameter method. Example: Scala // Scala program of repeated // parameters // Creating object object arr { // Main method def main(args: Array[String]) { // Creating a method with // repeated parameters def mul(x: Int*) : Int = { // Applying 'product' method to // perform multiplication x.product } // Displays product println(mul(Array(7, 3, 2, 10): _*)) } } Output: 420 In order to pass an array in the defined method, we need to put a colon i.e, : and _* mark after passing the values in the array. An example to show that Repeated Parameters are always the last parameter of the method defined. Example: Scala // Scala program of repeated // parameters // Creating object object str { // Main method def main(args: Array[String]) { // Creating a method with // repeated parameters def show(x: String, y: Any*) = { // using 'mkString' method to // convert a collection to a // string with a separator "%s is a %s".format(x, y.mkString("_")) } // Displays string println(show("GeeksforGeeks", "Computer", "Sciecne", "Portal")) } } Output: GeeksforGeeks is a Computer_Sciecne_Portal Here, format is used to format strings. Comment More info N nidhi1352singh Follow Improve Article Tags : Scala Scala Scala-Method Explore OverviewScala Programming Language3 min readIntroduction to Scala7 min readSetting up the environment in Scala3 min readHello World in Scala2 min readBasicsScala Keywords2 min readScala Identifiers3 min readData Types in Scala3 min readVariables in Scala3 min readControl StatementsScala | Decision Making (if, if-else, Nested if-else, if-else if)5 min readScala | Loops(while, do..while, for, nested loops)5 min readBreak statement in Scala3 min readScala | Literals4 min readOOP ConceptsClass and Object in Scala5 min readInheritance in Scala5 min readOperators in Scala11 min readScala Singleton and Companion Objects3 min readScala Constructors4 min readScala | Polymorphism5 min readScala | Multithreading3 min readScala this keyword2 min readMethodsScala | Functions - Basics3 min readAnonymous Functions in Scala2 min readScala | Closures3 min readRecursion in Scala4 min readMethod Overloading in Scala5 min readMethod Overriding in Scala8 min readLambda Expression in Scala4 min readScala Varargs2 min readStringsScala String4 min readScala | String Interpolation3 min readScala | StringContext2 min readRegular Expressions in Scala5 min readStringBuilder in Scala4 min readScala PackagesPackages In Scala4 min readScala | Package Objects3 min readChained Package Clauses in Scala3 min readFile Handling in Scala3 min readScala TraitScala | Traits7 min readScala | Sealed Trait4 min readScala | Trait Mixins3 min readTrait Linearization in Scala5 min readCollectionsScala Lists5 min readScala ListBuffer6 min readListSet in Scala6 min readScala Map5 min readScala | Arrays6 min readScala | ArrayBuffer4 min readScala | Tuple5 min readSet in Scala | Set-13 min readSet in Scala | Set-27 min readBitSet in Scala5 min readHashSet In Scala4 min readStack in Scala3 min readHashMap in Scala3 min readTreeSet in Scala4 min readIterators in Scala5 min readScala | Option3 min read Like