Swift

//: 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))

  

原文地址:https://www.cnblogs.com/Rinpe/p/5053911.html