Swift iOS Interview : All about Closures and Functions

Animesh Mishra
2 min readJan 3, 2019

Function : Functions are self-contained chunks of code that perform a specific task.

Function are having three types.

Global Function : Have name but not capture values.

Nested Function : Have name & Capture Value

Closure : (Special type of function) Don’t have name but capture value

#########################################

Closure : Closure are self contained block of functionalities that can be passed around & used in your code.

Closure can capture the reference of any constant & variable from the surrounding context in which closure defined.

Closure are similar to lambdas and blocks

#########################################

Escaping Closure : when a closure is passed to a function but called after function returns.

One way that a closure can escape is by being stored in a variable that is defined outside the function.

#########################################

Non-Escaping Closure : when a closure is passed to a function but called before function returns.

Non-Escaping closure are by default closure type.

--

--