In this tutorial, I will describe the difference between Function and Closure.
This is my first post in medium kindly forgive me for any type of mistakes. Also don’t miss to appreciate if you like this :) :) Happy reading
What is Function
The function is a set of statements organized together which performs a specific task. In swift, we declare function by using “func” keyword. The function can take 0 to many parameters. Every function has a name which describes the task that function performs
Example of function->
In the above function, we have just added all the elements of an array and return added number. The output of the above function will look like this -
What is Closure
Closure is self-contained blocks of functionality, that can be passed around and used in our code. This is similar to blocks in Objective-C and C.
Closures can capture and store references to any constants or variables from the context in which they are defined. We can say that closure is also a type of function.
Like functions, closures can accept parameters and return values.
We can also pass closure as a parameter to a function
Syntax of closure
The “in” keyword is used to separate the return Type and statements inside the closure.
Closure examples —
1. Simple Closure
When you run the above program, the output will be: “Hello, Closure”
In the above program, you have defined a closure simpleClosure. The type of the simpleClosure is inferred to be of () -> ()
because it doesn't accept parameter and does not return a value.It will only print
2. Closure which accepts a parameter
In the above program, the type of closure (String) -> ()
states that the closure takes an input of type String
but does not return value. You can use the value passed inside the statements of the closure by placing a parameter name followed by in
keyword.
3. Closure which returns the value
A closure can also return value, as functions. If you need to return value from closure, you must explicitly add the type to return inside braces ()
which is followed by ->
In the above program, we have defined type as simpleClosureWithParameterAndReturnType: (String) -> (String)
because Swift cannot automatically infer the closure that returns a value. The type (String) -> (String)
states that the closure takes an input of type String
and also returns a value of type String
.
Let’s write another example of adding all the elements of an array and return added numbers using a closure. Same as the example of function from pic1
4. Passing closure inside a function like completion handler
We can also pass closure inside a function like a completion handler. Check below. I am taking the example of the same program(Pic2) for which we have written function and closure which returns the value in the above pic(Pic2).
Below is the program for adding all the elements of an array and return added number bypassing closure inside a function, like completion handler
In the above program, we have just created a function which takes an array of numbers as input and in completion handler, we are getting added number. Below is the output
Difference between Function and Closure
- Function is declared using func keyword whereas Closure doesn’t have func keyword.
- Function has always name but Closure doesn’t have.
- Function doesn’t have in keyword but closure has in the keyword.