Swift - 30 - 可变参数

时间:2023-03-09 17:24:33
Swift - 30 - 可变参数
//: Playground - noun: a place where people can play

import UIKit

// 可变参数一定要放在所有定义参数的最后面, 和其他参数的定义方式一样, 只是多了3个点
func add(a:Int, b:Int, others:Int...) ->Int
{
var result = a + b;
for c in others { // 函数内可变参数会转换为一个数组
result += c;
}
return result;
} print(add(2, b: 3, others: 4, 5, 6, 7, 10, 100))