Swift3读后小感

函数和闭包


使用元组来让一个函数返回多个值。该元组的元素可以用名称或数字来表示。


func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) {

var min = scores[0]

var max = scores[0]

var sum = 0

for score in scores {

if score > max {

max = score

} else if score < min {

min = score }

sum += score }

return (min, max, sum)

}

let statistics = calculateStatistics(scores:[5, 3, 100, 3, 9])

print(statistics.min)

print(statistics.0)

你可能感兴趣的:(Swift3读后小感)