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.

Important to Note!

  • All Python functions return the special value None unless there is an explicit return statement with a value other than None.
  • Execution of the return statement 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

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.

# 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.

Powered by Forestry.md