There is no such thing as “returning nothing” in the Python function. If no explicit
return statement is used, Python treats it as returning
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))
def f(x): if x > 1: return x else: print("Function Return literally nothing") None print(f(0))
Python Function Return None Without Return Statement
The function does not have a
return statement. The function in that case implicitly returns
def func(): pass print(func())
Do comment if you have any doubts or suggestions on this Python return topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.