7.1. Closures with parameters

We can take the help of in keyword to pass parameters in a closure. For example, in the following code, we define a closure to take a person's name as a parameter and say hello to them:

let sayHelloTo = { (name:String) in
    print("Hello \(name)!")
}

//calling closure with parameter...
sayHelloTo("Donald")

It prints:

Hello Donald!

Do you find a difference between functions and closure in handling the parameter labels? 😉

Last updated

Was this helpful?