In Python, functions are first-class objects, which means you can treat them like any other variable. You can:
Here’s a simple example
def greet(name):
return f"Hello, {name}!"
def shout(name):
return f"HELLO, {name.upper()}!"
# This function takes another function as a parameter
def call_with_name(func, name):
return func(name)
# Passing 'greet' as a parameter
print(call_with_name(greet, "Alice")) # Output: Hello, Alice!
# Passing 'shout' as a parameter
print(call_with_name(shout, "Bob")) # Output: HELLO, BOB!
When you pass a function as an argument, you pass the function object itself — not the result of calling it.
That’s why we pass greet, not greet().
You can even use lambda functions for quick, inline behavior:
print(call_with_name(lambda n: f"Hi there, {n}!", "Charlie"))
# Output: Hi there, Charlie!
Would you like me to show how to return a function from another function too (that’s called a higher-order function)?