Playing Around with Closures in Swift- 4 (Part -1 )

Shakti Prakash
3 min readSep 20, 2018

--

According to the swift programming language

Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages.

In simple term closures are functions having no name .They simply capture and store reference to any constants and variables from the context in which they are defined .This is known as closing over those constants and variables.

Closure Expression Syntax

{ ( parameters) -> return type in

statements

}

Let’s start with a simple closure which prints a message

In the above example a simple closure is created and assigned to myfirstClosure .We can also say that a function with no name is created and we can call it like a normal function.

myfirstClosure()

Closures with parameters

Closures can accept parameters .To make the closure accept parameters we have to list them inside parentheses and then we have to write in keyword So that the closure main body can be started after this .And the example of closures with parameters is as follows .

let myfirstClosure = { (withParameter : String) in
print(“Finally i created my first Closure with \(withParameter)”)
}
myfirstClosure(“firstparameters”)

and the out put will be

Finally i created my first Closure with firstparameters

Closures with returning value type

Closures can also return value .So to make our closure return value we have to do little modification in our closure with parameters like Syntax .After listing the parameters in side parentheses we have to write ->datatype followed by In Keyword and lastly use return just like normal functions .And the example is as follows .

Closure as Parameters :-

We can also pass a closure like a parameter of a function just like how we pass string or integers as parameters of a function .But while doing so we have to do few things in the syntax .As we define each parameters type inside a function so a when a closure is used as parameters inside a function so we need to specify the Closure (parameter ) type as ()->datatype if we are returning nothing we can simple changed above syntax as ()->Void. here void mean nothing .And example is defined as below

import UIKitlet closure = {
print(“I am a simple closure “)
}
func simplefunctionWithClosures(closure:()->Void){
print(“Call the Closure”)
closure()
print(“Closure is called which is used as parameter of the function “)
}
simplefunctionWithClosures(closure: closure)

And the output is here

Trailing Closures

When we need to pass a closure expression to a function as the function final argument and the closure expression too long it can be useful to write it as a trailing closures .A trailing closure is written after the function call’s parentheses, even though it is still an argument to the function.When we use the trailing closure syntax, we don’t write the argument label for the closure as part of the function call.Here is what swift official doc says .

We will discuss some more about this in this articles .

--

--