def and return

Define a function with def. return hands a value back to the caller.

Syntax

def name(parameter1, parameter2=default):
return value
  • A function without return gives back None.

Example

def double(n):
    return n * 2

print(double(4))

Output:

8

Common mistakes

  • Printing a value is not the same as returning it.
  • Do not use a list or dictionary as a default value; use None and create it inside.

Practise it