Passing and returning functions

suggest change

The 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)

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents