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 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