Junior — Middle
Can a function be used as an argument for another function?
sobes.tech AI
Answer from AI
Yes, in Python functions are first-class objects, so they can be passed as arguments to other functions.
Example:
def greet(name):
return f"Hello, {name}!"
def call_function(func, arg):
return func(arg)
result = call_function(greet, "Alice")
print(result) # Outputs: Hello, Alice!
In this example, the greet function is passed as an argument to the call_function and called inside it.