Skip to main content

What can functions do?

Functions can serve a variety of purposes. These include:

Assigning new values to variables

x = 1

def add1():
x = x + 1

Calling other functions

def my_function():
some_function()
some_other_function()
some_other_other_function()

Calculate and return values

def add_two_numbers(a, b):
return a + b
def print_something():
print("Hello World!")
tip

As a rule of thumb:

  • When you want to truncate (shrink down) code, use a function.
  • When you want to make a block of code reusable, use a function.

All valid code can be put inside functions, and functions can return anything, from numbers to strings, to lists, to dictionaries, to booleans, to None, to even other functions.