Open In App

Scala Varargs

Improve
Improve
Like Article
Like
Save
Share
Report

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 :




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


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 :




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


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.



Last Updated : 28 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads