<aside>
🖤 没有明确定义返回值的函数,也有一个默认的返回值,类型为 Void
,值为一个空元祖 ()
。
</aside>
<aside> 👋 参数标签可以重名,但参数名称必须唯一。
</aside>
<aside> 👩💻 可变参数在函数体内会被当作一个确定元素类型的数组,函数可以有多个可变参数,所以可变参数的第一个参数必须要有一个标签,用来表明为可变参数的起始元素。
</aside>
func arithmeticMean(_ numbers: Double..., with weights: Double...) -> (Double, Double) {
var total: Double = 0
for number in numbers {
total += number
}
var weightTotal: Double = 0
for weight in weights {
weightTotal += weight
}
return (total / Double(numbers.count), weightTotal / Double(weights.count))
}
arithmeticMean(1, 2, 3, 4, 5, with: 3, 3, 3, 5, 5)
func swapTwoInts(_ a: inout Int, _ b: inout Int) {
let temporaryA = a
a = b
b = temporaryA
}
var someInt = 3
var anotherInt = 107
swapTwoInts(&someInt, &anotherInt)
print("someInt is now \\(someInt), and anotherInt is now \\(anotherInt)")
// 打印“someInt is now 107, and anotherInt is now 3”
func arithmeticMean(_ numbers: Double..., with weights: Double...) -> (Double, Double) {
var total: Double = 0
for number in numbers {
total += number
}
var weightTotal: Double = 0
for weight in weights {
weightTotal += weight
}
return (total / Double(numbers.count), weightTotal / Double(weights.count))
}
var a: Double = 1
var b: Double = 2
var c: Double = 3
var d: Double = 4
var e: Double = 5
arithmeticMean(a, b, c, d, e, with: a, b, c, d, e)