Closure

Closure is a feature which allows us to access a variable which is not in the scope of the current module. It is a way of implementing lexically scoped named binding, for example:

int add = x=> y=> x+y
int addTen = add(10)
addTen(5) // this will return 15

In this example, the add() function is internally called by the addTen() function. In an ideal world, the variables x and y should not be accessible when the add() function finishes its execution, but when we are calling the function addTen(), it returns 15. So, the state of the function add() is saved though code execution is finished, otherwise there is no way of knowing the add(10) value, where x = 10. We are able to find the value of x because of lexical scoping and this is called closure.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.111.1