Passing and returning functions

suggest change

The following function is returning another function as its result which can be later assigned to a variable and called:

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