DEV Community

Cover image for Variadic Function in Swift
NalineeR
NalineeR

Posted on

2 1 1 1 1

Variadic Function in Swift

The functions which takes Variadic parameter are called Variadic Functions.

Variadic parameters allow us to pass either single or multiple values based on our requirement.

Declaration - You just need to add Three Dots i.e. ... after param data type to make it variadic.

Example - Below function takes an Int as variadic param -

func getSum(values:Int...){
    let sum = values.reduce(0, +)
    print("Sum is-\(sum)")
}
Enter fullscreen mode Exit fullscreen mode

Now let's check how can we call it -
case 1 - Call it with single value -

getSum(values: 8)
Enter fullscreen mode Exit fullscreen mode

case 2 - Call it with multiple values -

getSum(values: 1,7)
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
sergeyleschev profile image
Sergey Leschev

Being able to pass multiple values to a function using a single parameter can save a lot of time and effort when working with larger datasets or more complex calculations.

Billboard image

📊 A side-by-side product comparison between Sentry and Crashlytics

A free guide pointing out the differences between Sentry and Crashlytics, that’s it. See which is best for your mobile crash reporting needs.

See Comparison

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay