Return Values
tags: #python/documentation/python_functions
Categorizing Functions by Return Behaviour
In Python, functions can be categorized into two main types based on their return behaviour: fruitful (or value-returning) functions and non-fruitful (or void) functions. The use of a return keyword in the function distinguishes a fruitful function from a non-fruitful function.
- All Python functions return the special value
Noneunless there is an explicit return statement with a value other thanNone. - Execution of the
returnstatement also signals the ends the function execution and 'sends back' the results of the function. - Therefore, you should NOT have any statements in a function after the return statement.
- DO NOT USE the
print()statement; will not return values of a fruitful function
return StatementIf you call a function that involves some sort of expression and do not store or display the results of the function, the return value disappears.
Non-Fruitful (Void, Procedural) Functions
Functions which perform a task but does not return or produce a result or value as a result of their execution. The return statement may be omitted or used without a value.
- Printing into the console
- Modifying data
# Used without value
def func():
<statement>
...
return None # or just return [empty expression]
# Omitted
def func():
<statement>
...
A return statement with no arguments is the same as return None.
Fruitful (Value-Returning) Functions
Fruitful functions are functions that return a value as a result of their execution by using the return keyword, explicitly.
def funct():
<statements>
...
return <expression>
The return keyword is a special statement that can be used inside a function or method to send the function result back to the caller.