Code Explanation:
๐น 1. Outer Function Definition
def outer(x):
✅ Explanation:
A function outer is defined.
It takes one argument: x.
This function will return another function.
๐น 2. Inner Function Definition
def inner(y):
✅ Explanation:
Inside outer, another function inner is defined.
It takes parameter y.
๐น 3. Using Outer Variable Inside Inner
return x + y
✅ Explanation:
inner uses:
y → its own parameter
x → from outer function
๐ This is called a closure:
Inner function remembers the value of x even after outer finishes
๐น 4. Returning Inner Function
return inner
✅ Explanation:
outer does NOT return a value directly
It returns the function inner itself
๐น 5. Calling Outer Function
f = outer(5)
๐ What happens:
outer(5) is executed
x = 5
Returns inner function
๐ Now:
f → inner (with x = 5 stored)
๐น 6. Calling Returned Function
print(f(3))
๐ What happens:
Calls:
inner(3)
Inside inner:
x = 5 (remembered from closure)
y = 3
Calculation:
5 + 3 = 8
๐ฏ Final Output
8

0 Comments:
Post a Comment