Group Group Group Group Group Group Group Group Group

Programming in Swift: Functions and Types · Challenge: Closures | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5429279-programming-in-swift-functions-and-types/lessons/11

I don’t understand the part about things that can only be done in a closure instead of a function. The Pythagorean theorem example shows
printResult({ (a, b) -> Double in
ab
}, 3, 4)
But can’t this be done with the following function?
func pythagorean(a: Double, b: Double) -> Double {
return ((a
a) + (b*b)).squareRoot()
}
pythagorean(a: 5, b: 3)

1 Like

Hi! You can certainly do that exact thing with a function. What you can’t do is call pythagorean and tell it to do something else with its parameters.

With a higher-order function like printResult, you can call the same function multiple times, but ask it to perform different operations each time via the closure. Hopefully you’ve continued watching this part of the course and have seen how useful that can be with map and other collection methods!