Home » Python » Python programs

Python program to define an empty function using pass statement

Python empty function using pass statement: Here, we are going to learn how to define an empty function by using pass statement in Python?
Submitted by IncludeHelp, on April 11, 2019

Prerequisite: pass statement in Python

An empty function is a function that does not contain any statement within its body. If you try to write a function definition without any statement in python – it will return an error ("IndentationError: expected an indented block").

Consider the given code,

# python code to demonstrate example of 
# pass statement

# an empty function definition without any statement
def myfunc():
    
# main code 
print("calling function...")
myfunc()
print("End of the program")

Output

  File "/home/main.py", line 8
    print("calling function...")
        ^
IndentationError: expected an indented block

See the output – there is no statement in the definition part of the function myfunc(), so python compiler considers the next statement (which is a “print” statement in this program) as a statement of the function definition. Thus, an error "IndentationError: expected an indented block" occurs.

Defining an empty function with "pass" statement

If there is no statement in the function i.e. we want to create it as an empty function – we can use pass statement. As we have discussed in the earlier post (pass statement in python) that, a pass statement is a null statement and it does nothing.

Python code for an empty function with pass statement

# python code to demonstrate example of 
# pass statement

# an empty function definition with pass statement
def myfunc():
    pass

# another function having statement
def urfunc():
    print("This is your function")
    
# main code 
print("calling function...")

# calling functions
myfunc()
urfunc()

print("End of the program")

Output

calling function...
This is your function
End of the program

See the output – there is no error in the program, it compiled successfully. We just wrote a pass statement with the empty function myfunc().






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.