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 :
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 :
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.