Junior — Middle
Explain what is meant by the term 'closure' in programming.
sobes.tech AI
Answer from AI
Closure is a function that remembers and can use variables from an outer scope even after the outer function has finished executing.
Example in Python:
def outer(x):
def inner(y):
return x + y # inner uses variable x from outer
return inner
f = outer(10)
print(f(5)) # Outputs 15
Here, inner is a closure that "captures" the variable x from the outer function and retains its value even after outer has completed.