//: > Experiment:
//: > Remove the day
parameter. Add a parameter to include today’s lunch special in the greeting.
//:
//: Use a tuple to make a compound value—for example, to return multiple values from a function. The elements of a tuple can be referred to either by name or by number.
//:
func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) {
var min = scores[0]
var max = scores[0]
var sum = 0
<span class="k">for</span> <span class="n">score</span> <span class="k">in</span> <span class="n">scores</span> <span class="p">{</span>
<span class="k">if</span> <span class="n">score</span> <span class="o">></span> <span class="n">max</span> <span class="p">{</span>
<span class="n">max</span> <span class="o">=</span> <span class="n">score</span>
<span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="n">score</span> <span class="o"><</span> <span class="n">min</span> <span class="p">{</span>
<span class="n">min</span> <span class="o">=</span> <span class="n">score</span>
<span class="p">}</span>
<span class="n">sum</span> <span class="o">+=</span> <span class="n">score</span>
<span class="p">}</span>
<span class="nf">return</span> <span class="p">(</span><span class="n">min</span><span class="p">,</span> <span class="n">max</span><span class="p">,</span> <span class="n">sum</span><span class="p">)</span>
} let statistics = calculateStatistics([5, 3, 100, 3, 9]) print(statistics.sum) print(statistics.2)
//: Functions can also take a variable number of arguments, collecting them into an array. //: func sumOf(numbers: Int...) -> Int { var sum = 0 for number in numbers { sum += number } return sum } sumOf() sumOf(42, 597, 12)
//: > Experiment: //: > Write a function that calculates the average of its arguments. //: //: Functions can be nested. Nested functions have access to variables that were declared in the outer function. You can use nested functions to organize the code in a function that is long or complex. //: func returnFifteen() -> Int { var y = 10 func add() { y += 5 } add() return y } returnFifteen()
//: Functions are a first-class type. This means that a function can return another function as its value. //: func makeIncrementer() -> (Int -> Int) { func addOne(number: Int) -> Int { return 1 + number } return addOne } var increment = makeIncrementer() increment(7)
//: A function can take another function as one of its arguments. //: func hasAnyMatches(list: [Int], condition: Int -> Bool) -> Bool { for item in list { if condition(item) { return true } } return false } func lessThanTen(number: Int) -> Bool { return number < 10 } var numbers = [20, 19, 7, 12] hasAnyMatches(numbers, condition: lessThanTen)
//: Functions are actually a special case of closures: blocks of code that can be called later. The code in a closure has access to things like variables and functions that were available in the scope where the closure was created, even if the closure is in a different scope when it is executed—you saw an example of this already with nested functions. You can write a closure without a name by surrounding code with braces ({}
). Use in
to separate the arguments and return type from the body.
//:
numbers.map({
(number: Int) -> Int in
let result = 3 * number
return result
})
//: > Experiment: //: > Rewrite the closure to return zero for all odd numbers. //: //: You have several options for writing closures more concisely. When a closure’s type is already known, such as the callback for a delegate, you can omit the type of its parameters, its return type, or both. Single statement closures implicitly return the value of their only statement. //: let mappedNumbers = numbers.map({ number in 3 * number }) print(mappedNumbers)
//: You can refer to parameters by number instead of by name—this approach is especially useful in very short closures. A closure passed as the last argument to a function can appear immediately after the parentheses. When a closure is the only argument to a function, you can omit the parentheses entirely. //: let sortedNumbers = numbers.sort { $0 > $1 } print(sortedNumbers)