How to Use Variadic Parameters in Swift?
Last Updated :
13 Sep, 2022
A function is a block of code that is used to perform some specified task. It is reusable and saves time and resources from writing the same code again and again. The syntax of the Swift function is flexible enough to create simple as well as complex functions. A function definition contains its name, parameters list, and return value. It is not necessary for a function to always contain a parameter list or return type, it depends upon the need of the user. In Swift language, a function supports three types of parameters: default parameters, in-out parameters, and variadic parameters. By default, a function contains constant parameters which are also known as default parameters, here the constant parameter means the value of the function cannot be changed and if we try to change then the compiler will give an error. The general syntax for creating a function is:
Syntax:
func functionName(parameterName: type) -> returnType{
// Body of the function
}
Now we will discuss variadic parameters.
Variadic Parameters
In Swift, variadic parameters are the special type of parameters available in the function. It is used to accept zero or more values of the same type in the function. It is also used when the input value of the parameter is varied at the time when the function is called. In the Swift function, when we are working with variadic parameters then it is necessary that the values passed to the variadic parameter must be used inside the function’s body as an array of a specific type. A single function can contain multiple variadic parameters and it is necessary that the parameter comes after the first variadic parameter that contains the argument label. It will make parameters unambiguous so that it is easy for the compiler to understand which argument is passed to which parameters(that is variadic and the parameter comes after the variadic parameter). In a function, a variadic parameter is created using three-period characters(…).
Syntax:
func functionName(_parameterName: type …) -> returnType{
// Function body
}
Example:
Swift
import Swift
func StudentResult(_ marks: Int ...) -> Int {
var sum = 0
for i in marks{
sum += i
}
return sum
}
var summ = StudentResult(23, 45, 66, 77, 88, 66)
print ( "Total marks of semester 1: " , summ)
|
Output:
Total marks of semester 1: 365
Variadic parameters with other parameters
In function, we can are allowed to use variadic parameters along with other parameters. When we are using a variadic parameter with other parameters then the variadic parameter is placed at the first position then the other parameters are used. It is necessary for the parameter that comes after the first variadic parameter must contain the argument label because it will make parameters unambiguous so that it is easy for the compiler to understand which argument is passed to which parameter either for variadic or the parameter that comes after the variadic parameter.
Syntax:
func functionName(_parameterName: type …, parameterName2: type, parameterName3: type) -> returnType{
// Function body
}
Example:
Swift
import Swift
func StudentResult(_ marks: Int ..., Sname: String ){
var sum = 0
for i in marks{
sum += i
}
print ( "Name of the student: " , Sname)
print ( "Total marks of semester 1: " , sum)
}
StudentResult(23, 45, 66, 77, 88, 66, Sname: "Rohan" )
|
Output:
Name of the student: Rohan
Total marks of semester 1: 365
Multiple Variadic parameters
A swift function can contain multiple variadic parameters starting from Swift 5.4. The earlier version of Swift doesn’t support multiple variadic parameters in the function, if you try to use them you will get a compiler error. In multiple variadic parameters, it is necessary that all the variadic parameters must contain the argument label because it will make parameters unambiguous so that it is easy for the compiler to understand which argument is passed to which parameters. If we do not use the argument label at the time of calling the function then the compiler assigned all the values to the first parameter and the remaining parameters remains unassigned.
Syntax:
func functionName(_parameterName1: type …, parameterName2: type…, parameterName3: type…) -> returnType{
// Function body
}
Example:
Swift
import Swift
func StudentResult(_ sem1: Int ..., sem2: Int ..., sem3: Int ..., sem4: Int ... ){
var sum1 = 0
var sum2 = 0
var sum3 = 0
var sum4 = 0
for i in sem1{
sum1 += i
}
for j in sem2{
sum2 += j
}
for k in sem3{
sum3 += k
}
for l in sem4{
sum4 += l
}
print ( "Total marks of semester 1: " , sum1)
print ( "Total marks of semester 2: " , sum2)
print ( "Total marks of semester 3: " , sum3)
print ( "Total marks of semester 4: " , sum4)
}
StudentResult(30, 45, 66, 77, 88, 66,
sem2: 45, 67, 34, 67, 89,54,
sem3: 98, 87, 86, 76, 78, 71,
sem4: 90, 80, 76, 71, 61, 52)
|
Output:
Total marks of semester 1: 372
Total marks of semester 2: 356
Total marks of semester 3: 496
Total marks of semester 4: 430
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...