Skip to content

Python function return nothing

  • by

There is no such thing as “returning nothing” in the Python function. If no explicit return statement is used, Python treats it as returning None.

def f(x):
    if x>1:
       return(x)
    else:
        # don't return anything

To literally return ‘nothing’ use pass, which basically returns the value None if put in a function(Functions must return a value, so why not ‘nothing’). You can do this explicitly and return None yourself though.

Python function returns nothing

Simple example code.

def f(x):
    if x > 1:
        return x
    else:
        pass


print(f(0))

or

def f(x):
    if x > 1:
        return x
    else:
        print("Function Return literally nothing")
        None


print(f(0))

Output:

Python function returns nothing

Python Function Return None Without Return Statement

The function does not have a return statement. The function in that case implicitly returns None.

def func():
    pass


print(func())

Output: None

Do comment if you have any doubts or suggestions on this Python return topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *