Skip to main content
March 23, 2026Noble Desktop/3 min read

Calling a Function with Python

Master Python Function Composition and Function Calls

Prerequisites

This tutorial assumes you have watched the previous video on composing functions. Understanding basic function creation is essential for following along with nested function calls.

Core Concepts Covered

Function Composition

Learn how to create reusable functions that perform specific calculations. Build modular code by breaking complex tasks into smaller functions.

Function within Function

Master the technique of calling existing functions from within new functions. Avoid code duplication by leveraging previously defined functionality.

Multiple Return Values

Understand how Python handles functions that return multiple values using tuples. Learn the data structure implications of complex return types.

Video Transcription

Hi, my name is Art, and I teach Python at Noble Desktop. In this video, I'll demonstrate how to call functions within other functions—a fundamental technique that enables modular, reusable code architecture.

Building on the function composition concepts from my previous video, let's work through a practical scenario: calculating the total area of a floor containing multiple rooms. This real-world example illustrates how function composition simplifies complex calculations.

First, we'll create a foundational function called "area" that accepts two parameters: length and width. The function calculates area using the formula length × width and returns the result. This single-responsibility function becomes our building block for more complex operations.

Now we'll apply this function to calculate individual room areas. Room 1 measures 3 × 5 square feet—a compact space. Room 2 spans 30 × 5 square feet, significantly larger. Room 3 covers 5 × 7 square feet. By calling our area function for each room, we maintain consistent calculation logic across all measurements.

To find the total floor area, we create a "floor" variable that sums all three room calculations: Room 1 + Room 2 + Room 3, yielding 200 square feet. This approach scales efficiently—whether you're calculating areas for 3 rooms or 300.

Now let's explore the core concept: calling functions within other functions. This technique, known as function composition, is essential for building sophisticated Python applications and data processing pipelines.

We'll create an "add" function that takes parameters A and B, returning their sum (A + B). Next, we'll build a "multiplication" function with the same parameter structure, returning A × B. These simple functions serve as components for our larger system.

Here's where function composition becomes powerful. Our "main" function accepts parameters A and B, then orchestrates calls to our existing functions. Instead of rewriting mathematical operations, we leverage our pre-defined add and multiplication functions—demonstrating the DRY (Don't Repeat Yourself) principle that's crucial in professional development.

Within the main function, we create an "addition" variable that calls our add function with parameters A and B. Similarly, our "multiplication" variable calls the multiplication function with the same parameters. This approach promotes code reusability and maintainability—critical factors in enterprise-level Python development.

The main function returns both the addition and multiplication results. Remember to execute all code cells in sequential order to ensure proper function definition and scope.

When we call main(5, 6), the function returns results from both internal operations: addition and multiplication. This demonstrates how a single function can coordinate multiple operations and return comprehensive results.

Since this function returns multiple values, Python automatically packages them as a tuple—an immutable sequence type. This behavior is particularly useful in data science and web development scenarios where functions need to return complex result sets. For a deeper dive into tuples and other Python data structures, I recommend watching my comprehensive videos on lists, strings, and tuples, which cover the fundamental data types every Python professional should master. Thank you for watching.

Building the Area Calculation Function

1

Define the Base Function

Create an 'area' function that takes length and width parameters and returns their product

2

Calculate Individual Rooms

Room 1: 3x5 square feet, Room 2: 30x5 square feet, Room 3: 5x7 square feet

3

Sum Total Floor Area

Add all room areas together to get the total floor area of 200 square feet

Room Area Breakdown

Room 1
15
Room 2
150
Room 3
35
Function Reusability

Instead of writing the same mathematical operations multiple times, create dedicated functions for add and multiply operations that can be called from within other functions.

Creating Nested Function Calls

1

Define Helper Functions

Create 'add' function for addition and 'multiplication' function for multiplication operations

2

Build Main Function

Create a 'main' function that calls the helper functions instead of duplicating the mathematical operations

3

Return Multiple Values

Return both addition and multiplication results, which Python automatically packages as a tuple

Cell Execution Order

Make sure you run all the cells in sequential order. Function definitions must be executed before they can be called by other functions.

Since this function returns more than one result, these results are returned in the form of a tuple.
Understanding how Python handles multiple return values is crucial for working with complex functions that perform multiple operations.

Next Learning Steps

0/3

Key Takeaways

1Functions can call other functions to avoid code duplication and improve modularity
2The area calculation example demonstrates practical function composition with real-world applications
3Room areas calculated: Room 1 (15 sq ft), Room 2 (150 sq ft), Room 3 (35 sq ft), totaling 200 sq ft
4Helper functions like 'add' and 'multiplication' can be reused within main functions
5Functions returning multiple values automatically create tuples in Python
6Proper cell execution order is critical when defining and calling functions in sequence
7Understanding tuples is essential for working with functions that return multiple results
8Function composition promotes cleaner, more maintainable code by breaking complex operations into smaller, reusable components

RELATED ARTICLES