Scala Varargs

Most of the programming languages provide us variable length argument mobility to a function, Scala is not a exception. it allows us to indicate that the last argument of the function is a variable length argument. it may be repeated multiple times. It allows us to indicate that last argument of a function is a variable length argument, so it may be repeated multiple times. we can pass as many argument as we want. This allows programmers to pass variable length argument lists to the function. Inside function, the type of args inside the declared are actually saved as a Array[Datatype] for example can be declared as type String* is actually Array[String].
Note :- We place * on the last argument to make it variable length.
Syntax : –


def Nameoffunction(args: Int *) : Int = { s foreach println. }

Below are some restrictions of varargs :

  • The last parameter in the list must be the repeating argument.
    def sum(a :Int, b :Int, args: Int *)



  • No default values for any parameters in the method containing the varargs.
  • All values must be same data type otherwise error.
    > sum(5, 3, 1000, 2000, 3000, "one")
    > error: type mismatch;
    found : String("one")
    required: Int
  • Inside the body args is an array, so all values are packed into an array

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of varargs
object GFG 
      
    // Driver code
    def main(args: Array[String])
    
              
        // Calling the function 
        println("Sum is: " + sum(5, 3, 1000, 2000, 3000)); 
    
      
      
    // declaration and definition of function 
    def sum(a :Int, b :Int, args: Int *) : Int =
    {
        var result = a + b
      
        for(arg <- args)
        {
            result += arg
        }
      
        return result
    }

chevron_right


Output :

Sum is: 6008

In above example we can see the last argument of the function is a variable length argument. here 1000 is variable length argument. argument arg is added to the result variable. the names argument is of type Integer.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of varargs
object GFG
    // Driver code
    def main(args: Array[String]) 
    {
      
        // calling of function     
        printGeek("Geeks", "for", "geeks")
  
    }
  
    // declaration and definition of function
    def printGeek(strings: String*) 
    {
        strings.map(println)
    }
  

chevron_right


Output :

Geeks
for
geeks

In above example as we have defined it using the * syntax so it is a variable argument. the names argument is of type String.



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.