Skip to main content

How to Pass a Function as an Argument in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

In Python, a function is a value like a number or a piece of text, so you can hand it to another function and let that function decide when to call it. The trick is to write the function's name without parentheses. This guide does it on Python 3.15.2.

Syntax​

Syntax
def <function>(<parameter>):
...


def <caller>(<step>, <value>):
return <step>(<value>)


<caller>(<function>, <value>)
PartWhat it means
<function>The function you pass. Its name on its own, without parentheses, is the function itself
<caller>A function that takes another function as one of its parameters
<step>The parameter that receives the function. <step>(<value>) calls whichever function was passed
<caller>(<function>, <value>)The call: the function's name goes in as an argument, like any other value

The Python tutorial explains why this works: "A function definition associates the function name with the function object in the current symbol table." It adds that "Other names can also point to that same function object and can also be used to access the function". The parameter <step> is such another name.

The Example​

shout adds an exclamation mark to a piece of text. run_step takes any function and some text, and calls the function on the text. The script passes shout to run_step.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

steps.py
def shout(text):
return text + "!"


def run_step(step, text):
return step(text)


print(run_step(shout, "hello"))

The steps.py script in a macOS-style code window: a shout function that returns the text with an exclamation mark, a run_step function that calls step on text, and a print of run_step with shout and hello.

  • run_step(step, text) doesn't know which function it will get. step(text) calls whichever one arrives in step.
  • run_step(shout, "hello") passes shout without parentheses, so run_step receives the function itself, not something shout returned.

Step 2: Run It​

Terminal
uv run python steps.py
Output
hello!

The terminal after uv run python steps.py: hello!

run_step called shout("hello") through its step parameter, and shout returned hello!. Because run_step calls whatever it receives, it works with any function that takes one piece of text. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Adding parentheses: run_step(shout(), "hello")Python calls shout straight away, with no text, before run_step even starts: TypeError: shout() missing 1 required positional argument: 'text'Pass the name alone: run_step(shout, "hello")

FAQ​

What does a function's name give without parentheses?​

The function itself. print(shout) printed <function shout at 0x10367f3d0>, and the next run printed <function shout at 0x104e173d0>: the number after at is where the function sits in memory, so it changes from run to run. The tutorial shows the same kind of output for its own fib function.

Can I pass a built-in function?​

Yes. Any function that accepts the argument works. With the same run_step, run_step(len, "hello") gave 5, and run_step(str.upper, "hello") gave HELLO.

Conclusion​

run_step(shout, "hello") handed the function shout to run_step, which called it through its step parameter and got hello! back. Written with parentheses, shout() ran too early, without its text, and failed.

Additional Resources​

  • Defining functions — how a function's name refers to the function object.
  • Callable — the glossary's word for anything you can call with parentheses.