# What is a closure?

A closure is a function. Like any other Scala function, a Closure may be pure or impure. It may be named or anonymous, but it is primarily a function. You might be wondering then why we call it a Closure. There is a small difference in Closure and a Function.
Let's try to understand it with a simple example.

```                                        ```
def getHike(salary:Double) = salary * p/100              ```
```

The above code is a function definition. It takes your salary as input. Then it calculates and returns your hike.

## Free Variable

Have you noticed the p in the above code? What is that?
That is the percentage of the hike. Where is the definition of p? I mean, we are not passing p as a parameter. We have not defined p as a local value. The value p does not have any definition or meaning within the function. So, what is the p?
The p is a free variable for this function. A Function that uses one or more free variables is known as a Closure. That is the only difference between a function and a Closure. A closures uses one or more free variables. If you try to compile the above function, You will get an error because the compiler cannot find the p.

```                                        ```
def getHike(salary:Double) = salary * p/100
/* Output:-
*/                 ```
```

So we still have one pending question. Where will the compiler find the p? The answer is straightforward. The Scala compiler looks into the so-called nearest local lexical environment, in which that function was defined and tries to find a binding. So, let me define p outside the function and try compiling the function once again.

```                                        ```
var p =10
def getHike(salary:Double) = salary * p/100
getHike(5000)
//res0: Double = 500.0                 ```
```

It worked. So, the compiler detects that the function does not have a local definition for p and it is not even part of the parameter list. So, the variable p is a free variable, and the function is a Closure. Then it starts looking for the p. It starts searching for p in the lexical environment. Since the compiler finds the p, It does not complain.

## More on Closures

1. What if the value of a free variable changes?
2. What if the closure modifies the value of a free variable?
3. Why Closure? What are the benefits of a Closure?

### What if the value of a free variable changes?

When you execute a closure, it takes the most recent state of the free variables. Let me show you that.

```                                        ```
var p =10
def getHike(salary:Double) = salary * p/100
getHike(5000)
//res1: Double = 500.0
p=20
getHike(5000)
//res2: Double = 1000.0                 ```
```

The first line defines the value of p as 10. The second line defines the closure. Then, I call the closure. It gives me a 10% of my salary. Then I change the value of p to 20 and call the closure once again. I get 20% hike. So, the closure takes the latest state of the free variable. Let me ask you another question. Is this closure pure? The answer is No because the free variable p is a var. If the free variable is a val, the closure is pure because we know that val is a constant. It will never change, and hence the function will give the same value for the same input parameter. In fact, the value directly depends on the parameters, and the closure becomes referentially transparent. So, a Closure may be pure or impure depending on the type of the free variable.

### What if the closure modifies the value of a free variable?

If a closure modifies the free variable, the changes are visible outside the closure.

```                                        ```
var p =10
def getHike(salary:Double) = {
p=p*2
salary * p/100
}
println(p)
//10
getHike(5000)
//res8: Double = 1000.0
println(p)
//20                 ```
```

The first line of the above code defines p as 10. We call the Closure and check the value of the p once again. It becomes 20. So the changes made inside the closure are passed back as well.

### Why closure? What are the benefits of a Closure?

We learned that in functional programming, you could pass functions and return functions. Don't you think that it is similar to OOP?
In the OOP approach, we pass objects around, and in Functional Programming, we juggle with functions. Both are the same kind of things. Isn't it? If you agree with this, You can easily argue that objects are more flexible than functions.
Why?
For certain use cases, Objects are more flexible because objects carry two things. They carry methods as well as the data elements. Whereas, a function is alone because It does not have any state or a data item. So, if we have a requirement, where we need to pass a bunch of states along with a function, how will you do it?
The answer to this problem is a Closure and the free variables. Let's look at an example to see it in action.
We looked at the getHike function. Let me give you a realistic requirement. I have a list of employee numbers. It is something like shown below.

```                                        ```
val l = (1001 to 1005).toList
l.map(getHike)                    ```
```

I want a function that I can apply to all of these ids and get the hike for all of them. It should work like the second line of code shown above. Here is the code for the getHike function.

```                                            ```
def getHike =  {
//Load employee and their current salary
val e:Map[Int,Double] = Map(1001->35000.00,
1002->43000.00,
1003->28000.00,
1004->54000.00,
1005->17000.00)
// Some logic to derive percentage for each employee
val p:Map[Int,Double]  = Map(1001 -> 10.00,
1002->12.00,
1003->7.50,
1004->6.80,
1005->20.00)
(empID:Int) => (empID, e(empID) * p(empID) /100.00)
}                    ```
```

There are many things in the code, but I want you to focus on the last line. The last line is an anonymous function body. The getHike is a Higher Order function because it returns another function. The below code demonstrates the main idea.

```                                                ```
val f = getHike
f: Int => (Int, Double) = <function1>
//Get Hike for an employee
f(1001)
//res10: (Int, Double) = (1001,3500.0)
//Get Hike for a non existant employee
f(1006)
```

The first line makes a call to getHike that returns a function. The returned function takes an integer and gives a tuple of an integer and double.
In the next line, we call the function f with an employee number. It will give you the hike.
Further, In the next line, we pass an employee number that doesn't exist and get an exception. Now, I have a question.
Where is the f looking for employee number?
Look at the code for getHike. We have e and p> defined there. They are local values for the getHike function. But they are free variables for the anonymous function body in the last line.
Now, here is the point that I want to make.
The anonymous function returned from the last line is a closure. It uses two free variables, e> and p>. When we return it from the getHike, it carries the state of the e> and the p> with it. So, f contains the data with it. Isn't it?.
It is just like an object in the object-oriented world. When you start practicing Functional Programming, you will realize that closures and their capability to carry the state is incredible. It saves you a lot of complicated and unnecessary code and simplifies your solution.

## You will also like:

Functional Programming

What is Functional Programming and why it is important?

Learning Journal

Scala Variable length arguments

How do you create a variable length argument in Scala? Why would you need it?

Learning Journal

Pure Function benefits

Pure Functions are used heavily in functional programming. Learn Why?

Learning Journal

Scala named arguments

Learn about named arguments and default values in Scala functions with examples.

Learning Journal

Anonymous Functions

Learn Scala Anonymous Functions with suitable examples.

Learning Journal