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
Define the Base Function
Create an 'area' function that takes length and width parameters and returns their product
Calculate Individual Rooms
Room 1: 3x5 square feet, Room 2: 30x5 square feet, Room 3: 5x7 square feet
Sum Total Floor Area
Add all room areas together to get the total floor area of 200 square feet
Room Area Breakdown
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
Define Helper Functions
Create 'add' function for addition and 'multiplication' function for multiplication operations
Build Main Function
Create a 'main' function that calls the helper functions instead of duplicating the mathematical operations
Return Multiple Values
Return both addition and multiplication results, which Python automatically packages as a tuple
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.Next Learning Steps
Essential for understanding tuples and other Python data types
Core Python data structures that support function return values
Build more complex examples with multiple nested function calls