Skip to content
Related Articles

Related Articles

Improve Article
How to write an empty function in Python – pass statement?
  • Difficulty Level : Basic
  • Last Updated : 04 May, 2016

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

This article is contributed by Shivam Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :