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.
Recommended Posts:
- Scala Tutorial – Learn Scala with Step By Step Guide
- Scala short <(x: Short): Boolean
- Scala short <(x: Char): Boolean
- Scala Extractors
- Scala | Partially Applied functions
- Scala String indexOf(String str) method with example
- Scala String contentEquals() method with example
- Scala Keywords
- Scala Int /(x: Int) method with example
- Scala Int /(x: Short) method with example
- Program to print Java Set of characters in Scala
- Scala Map size() method with example
- Scala SortedMap addString() method with a start, a separator and an end with example
- Scala Iterator addString() method with example
- Scala String substring(int beginIndex, int endIndex) method with example
- Scala | Functions Call-by-Name
- Program to convert Java list to an iterator in Scala
- Scala Set &() method with example
- Scala | Type Inference
- Program to convert Java set of Shorts to an Indexed Sequence in Scala
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.

