Closures are a special type of function that are created in-line so that you can actually pass them to another function.
I am going to break up a closure statement here so it is a little easier
let add: (Int, Int) -> Int
This is how you declare a closure it is essentially the same as declaring a variable but the type is a functions input types and an optional output. But, this is not a complete closure statement this is the same as
let myName: String
This doesn’t actually define what myName is just declares that it is a variable
Here would be a full closure statement. Which does look very similar to Swift Functions because it is really just a special type of function
let add: (Int, Int) -> Int = { (lhs: Int, rhs: Int) -> Int in
return lhs + rhs
}
What a Closure would most often be used for is passing into another function. This is how a function signature could look when using a Closure
func customAdd(lhs: Int, rhs: Int, function: (Int, Int) -> Int) -> Int{
return function(lhs,rhs)
}
which would then look like this when called
let addedValue: Int = customAdd(lhs: 20, rhs: 10, function: add)