Junior — Middle
Is it possible to return a different function from the current function in programming?
sobes.tech AI
Answer from AI
Yes, in programming, it is possible to return a different function from a function. This is one of the features of languages that support functions as first-class objects, such as Python.
By returning a function, you can create closures, function factories, or decorators.
Example in Python:
def outer_function(x):
def inner_function(y):
return x + y
return inner_function
add_five = outer_function(5)
print(add_five(3)) # Outputs 8
Here, outer_function returns the inner_function, which remembers the value of x from the outer scope.