How to Call a Function Inside Another Function in Python
Yes, you can call one Python function from another by writing the helper function’s name and parentheses inside the outer function’s indented body. Python pauses the outer function at that line, runs the helper, and then resumes the outer function on the next line. The examples below show the basic pattern, arguments, return values, methods, nested functions, and common mistakes.
What Happens When One Python Function Calls Another
A function definition creates a named block of code. Calling that name with parentheses runs the block.
When Python reaches a call inside an outer function, execution follows this order:
- Python starts running the outer function.
- Python reaches the helper function call.
- Python runs the helper function’s body from top to bottom.
- Python returns to the next line in the outer function.
Function bodies run from top to bottom, including after a called function returns, as explained in Stanford’s Python function guide.
The location of a function call determines when its work happens.
For example, if save_file() appears before send_email() inside an outer function, Python saves the file before it sends the email.
Each function also has its own local variables. A variable created inside the helper is separate from a variable with the same name inside the caller unless you explicitly pass or return a value.
The Solution: Call a Helper From an Outer Function
Use this table to choose the call form that matches what your helper needs to do.
| Situation | Call Inside the Outer Function |
|---|---|
| Helper needs no input | helper() |
| Helper needs input | helper(value) |
| Helper returns a result | result = helper(value) |
| You are calling an object method | object.method() |
Step 1: Define the Helper Function
Define the helper with def, a name, parentheses, and a colon. Indent the lines that belong to its body.
def foo():
print("Helper work is running.")
The def foo(): line defines a no-argument function named foo. Defining it does not run it yet.
You can place the helper above the outer function, which is usually easiest to read. More precisely, Python must have executed the def statement before it reaches the point where the helper is called.
Step 2: Put the Helper Call Inside the Outer Function
Write the helper call at the exact point where its work should occur.
def outer():
print("Before the helper")
foo()
print("After the helper")
The line foo() calls the helper. Because it is indented under outer(), it runs only when outer() runs.
Do not leave the call unindented:
def outer():
print("This is inside outer")
foo()
In that version, foo() is not part of outer(). It runs at the top level when Python executes the file.
Step 3: Call the Outer Function
Call the outer function to start the sequence.
outer()
With all three pieces in one file, the output is:
Before the helper
Helper work is running.
After the helper
That order confirms that Python entered outer(), ran foo(), and then continued with the remaining line in outer().
Pass Arguments and Get Return Values
A helper often needs information from the outer function or needs to send a calculated result back.
Pass Positional Arguments in the Right Order
Parameters are the names in a function definition. Arguments are the values you supply when calling it. Positional arguments match parameters by position, not by parameter name.
def foo(width, height, color):
print(f"{color}: {width} by {height}")
def create_box():
foo(17, 13, 'red')
Here, foo(17, 13, 'red') assigns 17 to width, 13 to height, and 'red' to color.
Keep the argument order aligned with the function definition. If the function expects (width, height, color), passing (color, width, height) produces the wrong result even if the values themselves are valid.
You can also use keyword arguments when clearer:
foo(width=17, height=13, color="red")
Keyword arguments match by parameter name rather than position.
Pass a Calculation Directly
An argument can be an expression, not only a variable. Python evaluates the expression before it calls the function.
print(4 + 5)
The expression 4 + 5 is evaluated first, then print() receives the result. The same rule works with your own helper functions:
def show_total(total):
print(total)
def checkout():
show_total(4 + 5)
Use this when the calculation is short and only needed once. Store the result in a variable first when a descriptive name makes the code easier to understand or when you need the value again.
Store a Helper’s Return Value
Use return when the helper calculates something that the outer function needs.
def score(correct, total):
return correct / total
def show_score():
x = score(6, 10)
print(x)
The line x = score(6, 10) runs score, receives its returned value, and assigns that value to x.
A print() call displays information but does not provide a useful result for later code. A return statement sends a value back to the caller.
Call Methods From Another Method
A method is a function attached to an object or class. Call it through the object using dot notation.
For an existing object named bit, this calls its move method:
bit.move()
Inside a class method, use self to call another method on the same object:
class Report:
def format_title(self, title):
return title.upper()
def render(self, title):
formatted_title = self.format_title(title)
print(formatted_title)
self.format_title(title) is the method equivalent of calling a helper function. Do not write format_title(title) by itself unless it is a standalone function in the current scope; Python needs self. to find the method belonging to that Report object.
Define a Function Inside Another Function
Python also allows a nested function definition:
def outer():
def helper():
print("Running the nested helper")
helper()
outer()
This works, but the nested helper exists only within outer(). Code outside outer() cannot call it directly.
Use a nested helper when it is specific to one outer function and does not need to be reused elsewhere. Use a top-level helper when multiple functions may need it or when you want simpler testing and reuse.
Avoid Common Function-Call Errors
These are the most common reasons a function call inside another function does not behave as expected.
| Symptom | Likely Cause | Fix |
|---|---|---|
NameError: name 'helper' is not defined |
Python has not executed the helper’s definition before the call runs. | Define the helper before calling the outer function. |
| The helper runs as soon as the file starts | The call is unindented and sits at the top level. | Move the call into the outer function’s indented body. |
TypeError about missing arguments |
The helper expects parameters, but the call does not supply them. | Pass the required arguments in the expected order or by keyword. |
The outer function gets None |
The helper prints a value but does not return one. | Add return value and assign the call result. |
| A method cannot be found | A class method was called like a standalone function. | Call it through self.method_name() or object.method_name(). |
A reliable structure looks like this:
def helper(value):
return value * 2
def outer():
result = helper(5)
print(result)
outer()
The helper is defined, called from the outer function, returns a value, and the outer function uses that value.
Test the Call Flow and Keep It Readable
For a quick test, add temporary print() statements immediately before and after the helper call.
def helper():
print("Inside helper")
def outer():
print("Entering outer")
helper()
print("Leaving outer")
outer()
If the messages appear in this order, the call flow is correct:
Entering outer
Inside helper
Leaving outer
Before keeping the code, remove temporary debugging output or replace it with logging where appropriate.
For maintainable code:
- Give helpers action-oriented names such as
validate_user(),build_payload(), orsave_record(). - Keep each helper focused on one task.
- Pass required data as arguments instead of relying on hidden global variables.
- Return values when the caller needs a result.
- Put calls inside the intended function body with consistent indentation.
- Call the outer function only after Python has defined the functions it needs.
Frequently Asked Questions
Is def a function?
No. def is Python’s keyword for defining a function. The code following def function_name(...): becomes the function body, while the function runs only when code calls its name with parentheses.
How Do I Call a Function From Another Python File?
Import the function from the other module, then call it normally. If helpers.py contains greet(), a separate main.py file can import and call it like this:
from helpers import greet
def run():
greet("Ada")
if __name__ == "__main__":
run()
Run the main file from the directory containing both files:
python main.py
Can You Call a Function Inside a Procedure in Python?
Yes. Python does not have a separate procedure keyword; a procedure is usually just a function that performs an action and does not explicitly return a useful value. A function can call another function whether it returns a value or simply performs work such as printing, saving data, or updating an object.
Start with one helper call inside a small outer function, then run the outer function and use output before and after the call to confirm that the helper runs between those lines.
