I will help you to learn how to call a function within a function in Python. As a developer, I faced a situation where I needed to call a function within a function during a project to one of my clients, then I explored more about the topic and I will share my findings in this tutorial along with suitable examples and screenshots.
Call a Function Within a Function in Python
Before we get into nested functions, let’s quickly review the basics of defining and calling functions in Python.
Read How to Use the arange() Function in Python?
Define a Function
In Python, you define a function using the def keyword, followed by the function name and parentheses. Here is a simple example:
def greet(name):
return f"Hello, {name}!"Call a Function
You call a function by using its name followed by parentheses. For example:
message = greet("John")
print(message)This will output:
Hello, John!You can refer to the screenshot below to see the output.

Check out How to Use the insert() Function in Python?
Nested Functions in Python
A nested function is simply a function defined inside another function. These inner functions are not accessible from outside the outer function, which can be useful for encapsulating functionality that is only relevant within the context of the outer function.
Define a Nested Function
Here is an example of a nested function:
def outer_function(city):
def inner_function(state):
return f"{city}, {state}"
return inner_functionIn this example, inner_function is defined within outer_function. The inner_function is not accessible from outside outer_function.
Check out How to Use the strip() Function in Python?
Call a Nested Function
To call a nested function, you first call the outer function and then call the inner function. Here is how you can do it:
get_location = outer_function("San Francisco")
location = get_location("California")
print(location)This will output:
San Francisco, CaliforniaYou can refer to the screenshot below to see the output.

Read How to Use the trim() Function in Python?
Examples of Nested Functions
Let’s explore some practical examples to understand the use cases of nested functions better.
Example 1: Calculate Sales Tax
Suppose you are developing a system to calculate the total price of an item including sales tax. You can use nested functions to encapsulate the tax calculation logic.
def calculate_total_price(base_price):
def calculate_tax(price):
tax_rate = 0.07 # 7% sales tax
return price * tax_rate
tax = calculate_tax(base_price)
total_price = base_price + tax
return total_price
price = calculate_total_price(100)
print(f"Total price: ${price:.2f}")This will output:
Total price: $107.00You can refer to the screenshot below to see the output.

Read How to Use the Floor() Function in Python?
Example 2: Format Addresses
Suppose you are working on an application that formats addresses. You can use nested functions to handle different parts of the address.
def format_address(street, city):
def format_city_state(city, state):
return f"{city}, {state}"
state = "NY" # Assume New York for this example
city_state = format_city_state(city, state)
return f"{street}\n{city_state}"
address = format_address("123 Main St", "New York")
print(address)This will output:
123 Main St
New York, NYCheck out How to Use the round() Function in Python?
Benefits of Using Nested Functions
Using nested functions offers several benefits:
- Encapsulation: Nested functions help in encapsulating functionality that is only relevant within the context of the outer function.
- Modularity: Breaking down a complex task into smaller functions makes the code more modular and easier to manage.
- Readability: Nested functions can improve the readability of the code by logically grouping related functionality.
- Scope Management: Variables defined in the outer function can be accessed by the inner function, which helps in managing the scope of variables effectively.
Read How to Use the repeat() Function in Python?
Common Issues and How to Avoid Them
While using nested functions can be beneficial, there are some common issues to be aware of:
- Complexity: Overusing nested functions can make the code harder to understand. Use them judiciously and only when it makes the code more readable.
- Performance: Nested functions can have a slight performance overhead. If performance is critical, consider the impact of using nested functions.
- Testing: Testing nested functions can be challenging since they are not accessible from outside the outer function. Ensure that your outer function is thoroughly tested to cover the inner functionality.
Check out How to Use the ceil() Function in Python?
Conclusion
In this tutorial, I have explained how to call a function within a function in Python. I discussed defining a function, calling a function, define a nested function and calling a nested function.I also covered examples of nested functions, benefits of using nested functions, common issues, and ways to avoid them.
You may read:
- How to Use the Python pop() Function?
- How to Use wait() Function in Python?
- How to Use the randint() Function in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.