Latest Post

How to use Swiftui searchable How to use SwiftUI AsyncImage What is Stored and Computed property in swift

 What is Closure?

Closure are the self contained blocks of functionality that can pass around and used in the code.

Syntax of Closure

{ (parameter) -> return type in



it contains the  parameter which are passed and the return type and statement which contains the logic

It Consist of 3 mode of argument they are

1.Return Type

2. Parameter

3. in (Optional)

types of Closure

1. Simple Closure


let returntype: (String) -> String = { name  -> in

    return  name



var name = {

  print("Hello, World!")


2. Closure With Parameter

Closure with parameter consist of parameter enclosed in the braces . Here Parameter should be passed in the argument. The Parameter can be of different data types

Example :

let user = { (name: String ) in




3.Closure With Return Type

In this Type of closure the consist of parameter with return type .Here parameter is passed to get the return type from the closure

Example :

let user = { (name: String ) -> String in

    return name



4. Trailing Closure

The closure that comes after the function call parentheses is known as the trailing closure. Rather of giving the parameter to the function, we pass it afterward.

func someFunction(closure:{



Example: hey{

print (“hey lead by code”)


6. Non-Escaping closure :

The non-escaping closure is used by default for the majority of the closures. All of the closures after Swift 3 are known as non-escaping closures.
When we give the argument arguments to a non-escaping closure function, the function conducts the appropriate operation and executes the result in the function body.

7. Escaping Closure:

escaping closure are always denoted by @escape . Here function passes a argument but preform the operation but it is called before it returns the value.

Leave a Reply

Your email address will not be published. Required fields are marked *