Friday, September 5, 2025
HomeLanguagesHow to write an empty function in Python – pass statement?

How to write an empty function in Python – pass statement?

In C/C++ and Java, we can write empty function as following

// An empty function in C/C++/Java
void fun() {  }

In Python, if we write something like following in Python, it would produce compiler error.




# Incorrect empty function in Python
def fun(): 


Output :

IndentationError: expected an indented block

In Python, to write empty functions, we use pass statement. pass is a special statement in Python that does nothing. It only works as a dummy statement.




# Correct way of writing empty function 
# in Python
def fun(): 
    pass


We can use pass in empty while statement also.




# Empty loop in Python
mutex = True
while (mutex == True) :
    pass


We can use pass in empty if else statements.




# Empty in if/else in Python
mutex = True
if (mutex == True) :
    pass
else :
    print("False")


RELATED ARTICLES

Most Popular

Dominic
32265 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS