How to write an empty function in Python - pass statement
Last Updated :
02 Jun, 2025
During the development of a Python program, we might want to create a function without implementing its behavior. In such cases, we can define the function and use the pass statement inside it to avoid syntax errors.
To write empty functions in Python, we use pass statement. pass is a special statement that does nothing. It only works as a dummy statement.
Empty Function with pass
Python
def fun():
pass # Function logic to be added later
# Calling the empty function
fun() # This does nothing for now
In this example, the function fun doesn’t perform any action because of the pass statement. However, it’s still defined and can be invoked like any other function.
Example with while Loop
A while loop repeatedly executes a block of code as long as the condition is true. If you want to create a while loop structure without implementing the logic right away, you can use the pass statement.
Python
i = 0
while i < 10:
pass # Placeholder for future code
i += 1
In this example, the while loop runs as long as i is less than 10. The pass statement indicates that the loop does nothing for now, but the loop still increments i.
Example with for Loop
A for loop iterates over a sequence of elements. You can use the pass statement to define a for loop without any operation for each iteration.
Python
for i in range(5):
pass # Placeholder for future code
Here, the for loop iterates over the range from 0 to 4, but it doesn’t perform any action because of the pass statement.
Example with if-else Statement
The if-else statement executes a block of code based on a condition. You can use the pass statement in both the if and else blocks to indicate that these blocks are empty.
Python
x = 10
if x > 5:
pass # Placeholder for future code
else:
pass # Placeholder for future code
In this example, the if condition checks if x is greater than 5. Both the if and else blocks contain the pass statement, indicating that no action is taken regardless of the condition.
Benefits of Using pass in Functions
- Helps Maintain Code Structure: We can set up the skeleton of your program early on without having to worry about implementing every detail at once.
- Keeps Syntax Valid: It prevents syntax errors when defining functions that have no body yet. Without pass, Python would raise an indentation error if the function body is left empty.
- Facilitates Incremental Development: We can focus on high-level logic first and implement specific functionality in small steps.
- Improves Readability and Collaboration: When working in teams, using pass clearly signals that a function is yet to be implemented, making the code easier to read and understand.
Similar Reads
How to pass an array to a function in Python Arrays in Python are implemented using the array module, which allows you to store elements of a specific type. You can pass an entire array to a function to perform various operations.Syntax to create an arrayarray(data_type, value_list)In this article, we'll explore how to pass arrays and lists to
3 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
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
Difference between continue and pass statements in Python Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by loop control statements. Loop control statements change execution from
3 min read