Passing and returning functions
suggest changeThe following function returns another function. That result can be stored in a variable and called. A call through a function value uses no argument labels, because a Swift function type such as (String, Int) -> String does not include them:
func jediTrainer () -> ((String, Int) -> String) {
func train(name: String, times: Int) -> (String) {
return "\(name) has been trained in the Force \(times) times"
}
return train
}
let train = jediTrainer()
train("Obi Wan", 3)
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents