1. Creating Closures
  2. Accepting Parameters
  3. Returning Values
  4. Closures as Parameters
  5. Trailing Closure Syntax

1. Creating Closures

Swift lets us use functions just like any other type such as strings and integers. This means you can create a function and assign it to a variable, call that function using that variable, and even pass that function into other functions as parameters.

IMPORTANT: Closures let us wrap up some functionality in a single variable, then store that somewhere. We can also return it from a function, and store the closure somewhere else.

let driving = {
    print("I'm driving in my car")
}
driving()

output

I'm driving in my car

2. Accepting Parameters

Always declare data type of parameters.

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

driving("Karachi")

output

I'm going to Karachi in my car

this is how a function takes input