Closure In Swift Part I

Ekramul Hoque
Good Morning Swift
Published in
5 min readJun 18, 2018

How to Create function without name !!

Introduction:

Closure is one of the most useful future of swift.In this Three Part Article I Will explain Closure from very basic to advance with real life example .

What is Closure :

Closure is nothing more than function just without name .It Mean Closure is a function which have no name in different language its call differently Like : Anonymous function,Callback,lambdas etc . In another way we can say closure is self contain block of functionality that can be passed around and used in your code.

Before dive in to closure first we create a function and convert it to a closure .It make our concept clear.

Simple Function:

So here is basic function which is as usual swift function syntax .Lets Create it on closure without name !!

Simple Closure :

So this closure is similar to “addFuntion “ .Just difference is that closure need to assign in any variable or constant become of closure don’t have name .its self contain block of Functionality . here let addition is a variable we assign closure on this variable let me give an example with a picture .

If we consider function and closure than we can illustrate closure and function like this picture .

Lets Discuss about closure syntax.

Basic Syntax :

closure start with a curly brackets and end with a curly .Here is Simple closure syntax :

{(argumentName:Type) -> ReturnTypein
// statement and others
//......
//.....
return
}

Here is closure basic syntax start by a curly brackets than argument name and its type and than return type than in key word than statement and than return .lets start some code in playground .

Example One :

This another basic example of closure which parameter take two value and do calculation and return it .if we execute this closure we can do like this .

let result = clasureOne(10, 50) //result should be 500

Example Two :

This closure take two string and return back those to string as a single string .BTW this closure also assign to closureTWo variable .

var fullName = closureTWo("Mahmudul", "Hasan") 
// fullName Variable now got "Mahmudul Hasan"

Create In Different way:

We know Swift is a type-safe language So in function or closure we need to define type of every variable or constant before initialise it either implicitly or explicitly.For this Feature we can create closure in many way Let Start ..

This addTwoNumber closure we define type before equal = and remove type from closure header so we can write This way .

We can omit return type from header too become swift smart enough to understand

Now We can remove return keyword from body too .

Now We can write it in single line Like this

Now very shorthand and its really short

So here is interesting swift closure understand it have two variable and add those and return back to the variable addTwoNumber.becaose of we defined its type explicitly .And swift have default argument name with $0 ,$1,$2,$3 …so on ..

Function Return Closure:

A Function can return a closure . Function can return closure two way directly and indirectly . Lets dive in to it first indirectly .

Indirectly :

Before make a function which will return a closure .We Will make a closure which will return by function.Lets create a closure welcome which is () →() type .

Now lets function which will return back this closure .

This function return welcome closure if we take it on a variable we will back get back this closure .lets implement …

its should print “Welcome” become welcomeResult now get this closure and this closure print welcome.

Another Example :

lets Create an closure of type (Int,Int,Int) →Int .

lets create function which will return this closure

now if we assign this closure by this function on a variable we will get this closure like this

Here returnClosure is send back calculator closure and give reference to it and result should be 100 ..

Directly:

Lets return closure directly from function body lets try it …

lets create function and in return we can write closure directly

this function return back a closure of type (Int,Int) →Int.

Now lets Assign it to a variable and test ..

Closure pass on to function:

We can pass closure in to a function as a parameter .Here giving example of passing parameter with two way directly and indirectly .

Indirectly:

First create a closure which will we passed to an function as a parameter.So that create a closure and assign it to sayHello constant .

this closure don’t take any parameter and don’t return anything .Lets create and function called takeClosure.And pass sayHello to takeClosure as a paramter.

This function have a parameter named closure and its type is ()->() .Our sayHello closure type is also same ()->().Lets pass it to function like this

So in console its must print “Hello”.

Another example :

Create a closure with the name multiplier and its toke to Int and return a their product.

Now Create a function which take a (Int,Int) →Int type closure as a parameter.

This function take closure now lets execute it and take result on a variable.

here takeClosureTwo function take three parameter two is Int and another is a closure .

Directly:

We can pass closure pass on the parameter directly now lets give an example:

Lets Create a function which can take a closure of type () →().

Now pass closure directly like this :

its should print “hello world!”. And here i pass closure directly when executing this closure .

Lets another Example:

lets create a function which take two value one Int value and another is a closure .Closure type is (Int) →Int.

Now implement this function and write closure in directly on when its implementing..

Now double should be 40 become we multiply it by 2 on closure .

Hope Its clear how to to pass closure directly and indirectly on function .

Conclusion :

Today is enough i will explain in next part about Trailing Closure, Capture list ,Completion handler.So thank you for staying with me if you like don’t forget to share and give me clap .

--

--