Step-by-step Execution:
Function outer(x) is defined
It takes one parameter x.
Inside outer, another function inner(y) is defined.
inner(y) returns the sum of x (from outer) and y.
Calling outer(10)
x = 10 is passed as an argument.
The function inner(y) is returned as a closure, meaning it "remembers" the value of x = 10.
Assigning f = outer(10)
Now, f is essentially inner(y) but with x = 10 already captured.
Calling f(5)
This is equivalent to calling inner(5), where y = 5.
inner(y) computes 10 + 5 = 15.
Final Output:
15
0 Comments:
Post a Comment