How to Call Multiple Functions in Python
Last Updated :
16 Dec, 2024
In Python, calling multiple functions is a common practice, especially when building modular, organized and maintainable code. In this article, we’ll explore various ways we can call multiple functions in Python.
The most straightforward way to call multiple functions is by executing them one after another. Python makes this process simple and intuitive each function call happens in the order it’s written.
Python
def func1():
print("Hello, World!")
def func2():
print("Goodbye, World!")
func1()
func2()
OutputHello, World!
Goodbye, World!
Explanation:
- In this example, we have two functions: func1() and func2().
- We call each function one by one. When executed, Python runs each function in the order it appears.
Let's take a look at other cases of calling multiple functions:
Calling Multiple Functions from Another Function
Sometimes, we may want to group multiple function calls together within a single function. This approach can make our code more organized and easier to manage.
Python
def func1():
print("Function One Called")
def func2():
print("Function Two Called")
def call_multiple():
func1()
func2()
call_multiple()
OutputFunction One Called
Function Two Called
Explanation:
- The call_multiple() function calls func1() and func2().
- When we call call_multiple(), both functions are executed in sequence, allowing us to group related functionality in one place.
Calling Functions in a Loop
When we need to call multiple functions that follow a similar pattern, it’s often more efficient to use a loop. This is particularly useful when we need to execute functions that take similar arguments or perform related tasks.
Python
def add(a, b):
return a + b
def multiply(a, b):
return a * b
functions = [add, multiply]
for func in functions:
print(func(3, 5))
Explanation:
- We store add and multiply functions in a list called functions.
- We then loop through this list and call each function with the arguments (3, 5). This approach is efficient because it lets us call each function without repeating the same code.
Calling Functions with Arguments
When functions require arguments, we can still call multiple functions in sequence or from within another function. Passing the right parameters ensures that each function behaves as expected.
Python
def func1(name):
print(f"Hello, {name}!")
def func2(name):
print(f"Goodbye, {name}!")
# Calling functions with arguments
func1("Alice")
func2("Bob")
OutputHello, Alice!
Goodbye, Bob!
Explanation:
- Both func1() and func2() require a name argument.
- We pass different names to each function when we call them. This demonstrates how we can use functions with parameters while calling multiple functions in sequence.
Calling Functions in Parallel (Concurrency)
In some situations, we might want to execute multiple functions concurrently, especially when dealing with tasks that are independent of each other. Python provides several tools for concurrency, such as the threading and multiprocessing modules, as well as asynchronous programming with asyncio.
Python
import threading
def func1():
print("Task One Started")
def func2():
print("Task Two Started")
thread1 = threading.Thread(target=func1)
thread2 = threading.Thread(target=func2)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
OutputTask One Started
Task Two Started
Explanation:
- We create two threads, thread1 and thread2, each responsible for calling func1() and func2(), respectively.
- By calling start(), both functions begin executing concurrently. This allows us to run them at the same time, rather than sequentially.
- The join() method ensures that the main thread waits for both threads to complete before proceeding.
Similar Reads
How to call a function in Python Python is an object-oriented language and it uses functions to reduce the repetition of the code. In this article, we will get to know what are parts, How to Create processes, and how to call them.In Python, there is a reserved keyword "def" which we use to define a function in Python, and after "de
5 min read
How to Call a C function in Python Have you ever came across the situation where you have to call C function using python? This article is going to help you on a very basic level and if you have not come across any situation like this, you enjoy knowing how it is possible.First, let's write one simple function using C and generate a
2 min read
How to Break a Function in Python? In Python, breaking a function allows us to exit from loops within the function. With the help of the return statement and the break keyword, we can control the flow of the loop.Using return keywordThis statement immediately terminates a function and optionally returns a value. Once return is execut
2 min read
How to Recall a Function in Python In Python, functions are reusable blocks of code that we can call multiple times throughout a program. Sometimes, we might need to call a function again either within itself or after it has been previously executed. In this article, we'll explore different scenarios where we can "recall" a function
4 min read
Python | How to get function name ? One of the most prominent styles of coding is following the OOP paradigm. For this, nowadays, stress has been to write code with modularity, increase debugging, and create a more robust, reusable code. This all encouraged the use of different functions for different tasks, and hence we are bound to
3 min read
How to Define and Call a Function in Python In Python, defining and calling functions is simple and may greatly improve the readability and reusability of our code. In this article, we will explore How we can define and call a function.Example:Python# Defining a function def fun(): print("Welcome to GFG") # calling a function fun() Let's unde
3 min read
Using User Input to Call Functions - Python input() function allows dynamic interaction with the program. This input can then be used to call specific functions based on the user's choice .Letâs take a simple example to call function based on user's input .Example:Pythondef add(x, y): return x + y # Add def sub(x, y): return x - y # Subtract
2 min read
How to run same function on multiple threads in Python? In a large real-world application, the modules and functions have to go through a lot of input and output-based tasks like reading or updating databases, communication with different micro-services, and request-response with clients or peers. These tasks may take a significant amount of time to comp
3 min read
Pass a List to a Function in Python In Python, we can pass a list to a function, allowing to access or update the list's items. This makes the function more versatile and allows us to work with the list in many ways.Passing list by Reference When we pass a list to a function by reference, it refers to the original list. If we make any
2 min read
How to Install functools32 in Python The Functools is one such module in Python, which is used for higher-order functions i.e., functions that can act on or return other functions. In general, any callable object can be treated as a function for the purposes of this module. How to install functools32 in Python The functools32 is a back
2 min read