0Pricing
Swift Academy · Lesson

Parameters

Parameters

Introduction

Hello,

In this section, we will learn about the useful features of parameters in Swift language.

The first we will learn are variadic parameters.

A variadic parameter is a parameter that can take 0 or more values ​​of the same type. It can be used when we cannot predict exactly how many parameters it will take.

example

A function can have a maximum of one variadic parameter. 

Let's do an example.

func arithmeticMean(_ numbers: Double...) -> Double {
    var total: Double = 0
    for number in numbers {
        total += number
    }
    return total / Double(numbers.count)
}

var result = arithmeticMean(1, 2, 3, 4, 5)
print(result)
// returns 3.0, which is the arithmetic mean of these five numbers
result = arithmeticMean(3, 8.25, 18.75)
print(result)
// returns 10.0, which is the arithmetic mean of these three numbers
← Back to Swift Academy