Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to return void from Python function?

I have this code

def myfoo1():
  print "Hi Everyone"
def my_foo2():
  print "Hi Everyone"
  return
def my_foo3():
  print "Hi Everyone"
  return None

Why are the functions in this code all returning None in this code?


1 Answer
Rajendra Dharmkar

Since Python is dynamic-typed and you don't specify a return type when defining a function, then you can return anything with any type, that includes None which is the default return value (when you don't return anything, the function actually returns None at the bottom of function)

Functions like this are called void, and they return None, Python's special object for "nothing".

Also a simple return is equivalent to return None if you want to do it in the middle of function.

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.