Scala Closure

In Scala, a function whose return value depends on one or more variables declared outside the function, is called as closure.In other term, we can say that function and variable declared outside are bound to each other.

Let’s understand this with an example. We have defined a function which performs a sum of 2 variables – first its arguments and second variable declared outside.

Function return value will change if the value of variable that is declared outside , is changed. Let’s change the value of b as 5 and see the result.

We can pass the function sum to other methods and functions as shown below. We have created another function printSum accepting same signature of function as its parameter along with an Int parameter.

Scala - Function with named parameters
Scala - String
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

Leave a Reply

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