Scala | Repeated Method Parameters

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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))
    }
}

chevron_right


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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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): _*))
        }
    }

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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"))
        }
    }

    chevron_right

    
    

    Output:

    GeeksforGeeks is a Computer_Sciecne_Portal
    

    Here, format is used to format strings.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.