Call Function from Another Function in Python

1. Introduction

In Python, calling a function from another function is a common practice that enhances modularity and reusability. We’ll explore various ways to do this, with complete examples and detailed explanations. Our focus will be on understanding how these methods work, their use cases. As an example, let’s look at a scenario where we have a function add_numbers to add two numbers and another function print_result to call add_numbers and display the output.

2. Understanding the Stack in Function Calls

Before diving into different methods of calling functions, let’s understand how Python handles these calls internally using a stack.

2.1. The Role of the Stack

Python uses a stack, a first-in-last-out data structure, to manage function calls. When a function is called, it is pushed onto the stack. The stack holds the reference to where the program needs to return after executing the function. Once the function completes its execution, it is popped from the stack, and the program execution returns to the point where the function was called.

This mechanism is crucial for keeping track of function calls, especially when dealing with nested functions, as it ensures that each function returns control to the function that called it.

3. Basic Function Calling

3.1. Defining a Simple Function

A basic function in Python can be defined using the def keyword. Let’s define our add_numbers function:

This function takes two parameters, a and b, and returns their sum.

3.2 Calling a Function in Python

To call a function in Python:

  • Write a test() function, which prints a message.
  • Call the function defined in the previous step.
To call a function, we have to define that function using the def keyword, so we used that to define the test() function. Then, inside the test() function, we used the print() function to print a message, which would be printed on the execution of this function.

In function calls, we have two terms; Calling Function and Called Function. Here, the Calling Function is the function which invokes another function while the Called Function is the function which is being invoked.

Now, you may have a question about how the function execution works. How to decide where to return, particularly in the nested function call, which we will learn in the next section? The stack, the first-in-last-out data structure, handles function calls. How? Let’s see the following.

Call function in Python

In the above diagram, we had a function test(); when we called it, it was pushed to the stack to hold the reference to know where to return. Then, it executed the test() function and returned.

After that, the function was popped from the stack and returned to a point where it left the program execution and continued.

Now, you might be wondering why to struggle with all these and write functions; it is because the functions serve us with the following features:

  • It avoids code repetition, encourages code reusability and thus saves time, money, and memory.
  • It assists in hiding the code and lets us understand the modules efficiently.
  • Functions help us to break down big problems into smaller modules.
  • Functions can only be defined using the def keyword and a colon followed by a function name.
  • The statements of a function can only be executed with the function name.

You can read this to see what characters are allowed while writing the name of the function in Python. Now, assume we have a situation where we have to call a function from another. The concept would be the same, and a stack data structure would be used. Let’s see how?

3.3. Calling a Function from Another Function

To call function from another function in Python:

  • Write a function_A() function, which prints a message and returns.
  • Write another function_B() function which prints a message, calls function_A() that we created in the previous step and returns.
  • Now, call function_B() function.
Here, we wrote two functions, function_A() and function_B(), both had one print statement to print a message, but we also called function_A() from function_B().

Nothing happened until we reached the statement, where called function_B as function_B(). As soon as we called function_B(), it was pushed to the stack, and the execution of the function_B started. It printed the statement I am function B. and called function_A as function_A().

Now, the function_A is also pushed to stack and execution of function_A started; we got another statement printed on the console as I am function A.. Once we are done with function_A, it would be popped from the stack and returned where we left the function_B.

As we can see in the above code, we had a return statement after calling function_A, so the function_B also popped from the stack and returned where we left the program after calling the function_B.

So now, we only have one print statement to be executed, which printed I am now outside the functions. on the console. At this point, we had three statements printed on the console and the stack was also empty.

Let’s visualize the execution of the above program in the following diagram.

Call function from another function in Python

Let’s take another example where we will create print_result, which calls add_numbers:

Output:

Explanation:

  • add_numbers: Takes two parameters a and b, and returns their sum.
  • print_result: Calls add_numbers with x and y as arguments, then prints the result.

Let’s understand this call in terms of stack:

When print_result is called, it is pushed onto the stack. Inside print_result, add_numbers is called, which is then pushed onto the stack. Once add_numbers completes, it’s popped off the stack, and control returns to print_result, which is then also popped off after execution.

3.4. Importance of Function Calls

This approach demonstrates the basic principle of function calls in Python. It’s useful for separating logic (calculating the sum) from its application (printing the result), thus promoting code reusability and clarity.

4. Nested Function Calls

Nested functions are functions defined within other functions. They are useful for organizing code and encapsulating functionality.

Example:

Explanation:

  • inner_addition: A function defined inside outer_function. It’s only accessible within outer_function.
  • outer_function: Calls inner_addition and prints the result.
Nested functions are particularly useful for creating helper functions that are only relevant within the context of a larger function.

5. Passing Functions as Arguments

Python supports passing functions as arguments to other functions, known as higher-order functions.

Example:

Explanation:

  • perform_operation: Accepts a function func and two arguments x and y. It then calls func with these arguments.
  • add_numbers is passed as an argument to perform_operation.

This method is powerful for creating flexible and reusable code structures, like in functional programming.

6. Using Lambdas and Higher-Order Functions

Lambda functions allow for creating small, anonymous functions in a concise manner.

Example:

Output:

Explanation:

  • A lambda function lambda a, b: a * b is passed to perform_operation. This lambda multiplies two numbers.
  • The lambda is used for a one-time, inline operation without needing a named function.

Lambda functions are useful for short, on-the-fly operations where a full function definition would be unnecessarily verbose.

7. Callback Functions

Callback functions are passed to other functions and are executed at a specific time.
Example:

Output:

Explanation:

  • do_math: Takes two functions as arguments – operation and callback.
  • operation is a function that performs some computation.
  • callback is a function that is called with the result of operation.

Callback functions are widely used in scenarios like event handling, asynchronous processing, and in situations where the timing of a function call is crucial.

8. Calling a Function from Another Function within the Same/Different Classes

In object-oriented programming (OOP), classes are blueprints for creating objects. These objects can have methods (functions) that define their behavior. Calling a function from another function within the same or different classes is a common practice in OOP, enabling interaction between different parts of a program.

8.1. Within the Same Class

Example:

Explanation:

  • Calculator class has two methods: add and multiply_and_add.
  • multiply_and_add performs a multiplication and then uses self.add to add another number to the result.
  • self.add refers to the add method within the same class.

Output:

8.2. Between Different Classes

Example:

Explanation:

  • Multiplier class has a method multiply for multiplication.
  • AdvancedCalculator has a method multiply_and_subtract and an instance of Multiplier.
  • multiply_and_subtract calls self.multiplier.multiply to multiply two numbers, then subtracts a third number from the result.

Output:

8.3. Importance in Object-Oriented Programming

Calling functions from other functions within the same or different classes is fundamental in OOP. It allows for:

  • Encapsulation: Methods can be used to manipulate an object’s state while keeping its details hidden.
  • Code Reusability: Common methods can be defined in one class and reused in others.
  • Modularity: Breaking down complex tasks into simpler, manageable methods.

9. Conclusion

Understanding how to call a function from another function in Python is a fundamental skill that enhances the readability, maintainability, and organization of code. This article explored various methods for doing so, including basic function calls, nested functions, passing functions as arguments, using lambda functions, callback mechanisms, and calling functions within and across classes. Each method serves different purposes and choosing the right one depends on the specific requirements of the problem at hand. For beginners, mastering these concepts opens up a world of efficient and effective programming in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *