Return Values and Function Types
Functions may return a value back to its caller and those which are more procedural in nature do not explicitly return anything at all. Languages which treat procedures as functions usually have a special type or value name for functions that “return nothing.” These functions default to a return type of “void” in C, meaning no value returned. In Python, the equivalent return object type is None.
The hello() function acts as a procedure in the code below, returning no value. If the return value is saved, you will see that its value is None:
>>> def hello(): print "hello functions" >>> var1 = hello(); hello functions >>> print var1 None >>> type(var1) <type 'NoneType'>
Also, like most other languages, you may return only one value/object from a function in Python. One difference is that in returning a container type, it will seem as if you can actually return more than a single object. In other words, you can’t leave the grocery store with multiple items, but you can throw them all in a single shopping bag which you walk out of the store with, perfectly legal.
>>> def foo(): return ['xyz',1000000,-98.6] >>> def bar(): return 'abc',[42,'python',"snake"]
The foo() function returns a list, and the bar() function returns a tuple. Because of the tuple’s syntax of not requiring the enclosing parentheses, it creates the perfect illusion of returning multiple items. If we were to properly enclose the tuple items, the definition of bar() would look like:
def bar(): return ('abc', [4-2j, 'python'], "snake")