From 3124c4e0e014f9cad8759e8ae7c1d3e482125405 Mon Sep 17 00:00:00 2001 From: TheLittleBoy Date: Sat, 20 Sep 2014 16:52:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/chapter1/02_a_swift_tour.md | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/source/chapter1/02_a_swift_tour.md b/source/chapter1/02_a_swift_tour.md index 6aa0ce8f..a95b5732 100755 --- a/source/chapter1/02_a_swift_tour.md +++ b/source/chapter1/02_a_swift_tour.md @@ -233,13 +233,28 @@ greet("Bob", "Tuesday") > 练习: > 删除`day`参数,添加一个参数来表示今天吃了什么午饭。 -使用一个元组来返回多个值。 +使用元组来让一个函数返回多个值。该元组的元素可以用名称或数字来表示。 ```swift -func getGasPrices() -> (Double, Double, Double) { - return (3.59, 3.69, 3.79) +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) } -getGasPrices() +let statistics = calculateStatistics([5, 3, 100, 3, 9]) +statistics.sum +statistics.2 ``` 函数可以带有可变个数的参数,这些参数在函数内表现为数组的形式: