1. Using closures as parameters when they accept parameters
  2. Using closures as parameters when they return values
  3. Shorthand parameter names

Advanced Closures

  1. Closures with multiple parameters
  2. Returning closures from functions
  3. Capturing values

1. Using closures as parameters when they accept parameters

func travel(action: (String) -> Void) {
    print("I'm getting ready to go.")
    action("Karachi")
    print("I arrived!")
}

travel { (place: String) in
    print("I'm going to \(place) in my car")
}

output

I'm getting ready to go. I'm going to Karachi in my car I arrived!

2. Using closures as parameters when they return values

func travel(action: (String) -> String) {
    print("I'm getting ready to go.")
    let description = action("London")
    print(description)
    print("I arrived!")
}

travel { (place: String) in
    return "I'm going to \(place) in my car"
}

output

I'm getting ready to go. I'm going to London in my car I arrived!

3. Shorthand parameter names