All courses › Programming with Python › Functions and return

Functions and return

A function is defined with def and can take parameters, often with default values. return ends the function and sends a value back to the caller. Without return, the function gives None.

def f(a,b=c):…return e\text{def } f(a, b{=}c): \ldots \text{return } eff returns the value of ee; bb gets default cc if not supplied

Symbols

a, ba,\ bparameters
ccdefault value for bb
eethe expression returned

Example

def f(a, b=2): return a * b

f(3) uses the default: 3⋅2=63\cdot 2 = 6.

A function with no return statement always gives None, not 0 or an empty string.
Practise functions and data structures for free →

← List comprehension · Dictionary (dict) →

Part of Programming with Python: Functions and data structures.