Python program to define an empty function using pass statement

Here, we are going to learn how to define an empty function by using pass statement in Python? By IncludeHelp Last updated : January 05, 2024

Prerequisite: pass statement in Python

Empty function

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

Example

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

The output of the above program is:

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().

To understand the above programs, you should have the basic knowledge of the following Python topics:

Python Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.