Updated April 15, 2023
Introduction to Scala Closure
Scala Closures are the functions whose return values are dependent on the value of one or more free variables that it uses. The variables over which the function is dependent on is defined outside of the function. These free variables are not defined in the function neither they are used as a parameter over, neither are free variable bound to function with valid values. Closure Function takes the value of most recent free variables and evaluates accordingly.
Syntax:
Let us check the syntax for scala Closure Function:
Var a = 10 // any free variable defined
Def function_name(b:Int) //parameter set for Function
{
//Body of the function
Println(a+b) //The value dependent on the free variable
}
By this way, we can create a closure function in Scala.
Closure Functioning
A Closure function reference to the non-local variables of that function has access for all the variables that is outside its immediate lexical scope. So it takes the variable that was referenced earlier in the code and use that variable within the function to give the result. Even if that value is changed it will take the updated value and give the updated result.
Let us know what actually closures are, Closures are basically a combination of functions that have access to the surrounding state, this surrounding state is also called as lexical Environment. So closure gives us access of outer function from the scope of the inner one.
scala>var g = 10
g: Int = 10
scala>defadd(b : Int){
| println(g+b)
| }
add: (b: Int)Unit
scala>add(5)
15
scala>add(6)
16
scala> g = 20
g: Int = 20
scala>add(6)
26
scala>add(5)
25
Output:
From this above snippet of the code, we see how changing only the value of the external variable changes the value of the function that uses that. So this is the magic behind the Scala Closure Function.
The benefit of Using Closures Functions
A major benefit of having closure function is the concept of data encapsulation plus data persistence. Since the variables defined have a scope and if they are defined inside a function they will have a local scope, but with the help of closure function, we have defined a global variable and can use it inside the function also.
So by this, we can have those variables that are available even when the function task is finished.
Examples of Scala Closure
These closure functions can be also used for String as their data type. Let us see an example with a function taking a string as an argument.
1. Passing String as Closure in Scala
scala>var add = "Bangalore"
add: String = Bangalore
scala>defdesc( a : String)
| {
| println(" I am a "+a +", Working in "+add)
| }
desc: (a: String)Unit
scala>desc("Software Er")
I am a Software Er, Working in Bangalore
Output:
2. Passing Function as Closure in Scala
We can even pass the function too inside the function and then call them up on other functions. Let us check that with an Example:
scala>var add = "Bangalore"
add: String = Bangalore
scala>defdesc( a : String)
| {
| println(" I am a "+a +", Working in "+add)
| }
desc: (a: String)Unit
scala>desc("Software Er")
I am a Software Er, Working in Bangalore
scala>def desc2( r:String =>Unit , b:String)
| {
| desc(b)
| }
desc2: (r: String => Unit, b: String)Unit
scala> desc2(desc,"person")
I am a person, Working in Bangalore
Output:
3. Passing Integer Array as Closure
We can also pass an Array as a closure to a function in scala.
Let us check that with an example:
scala>val a = Array(2,4,6)
a: Array[Int] = Array(2, 4, 6)
scala>val show =(b:Int) =>println("The Array values using clousre is "+b)
show: Int => Unit = <function1>
scala>for(i<- 0 to a.length
length lengthCompare
scala>for(i<- 0 to a.length)
| {
| show(a(i))
| }
The Array values using clousre is 2
The Array values using clousre is 4
The Array values using clousre is 6
java.lang.ArrayIndexOutOfBoundsException: 3 [ here we will get an exception]
scala>for(i<- 0 until a.length)
| {
| show(a(i))
| }
The Array values using clousre is 2
The Array values using clousre is 4
The Array values using clousre is 6
Output:
So from these above examples, we saw the use of closure function in scala.
Conclusion
From the above article, we saw how closure function works, we saw how the value of an external variable is used in a function. Scala Closure function is well used for object-oriented programming in scala and very useful for high order project creation. With the help of examples, we use how we can use closure function in code. So this is how Scala Closure is used in SCALA PROGRAMMING.
Recommended Articles
This is a guide to Scala Closure. Here we also discuss the introduction and benefit of using closures functions along with closure function and examples. You may also have a look at the following articles to learn more –