Skip to content

Functions

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.

In Python, a function is defined using the def keyword:

def my_function():
print("Hello from a function")

To call a function, use the function name followed by parenthesis:

def my_function():
print("Hello from a function")
my_function()

Information can be passed into functions as arguments. Arguments are specified after the function name, inside the parentheses.

def greet(name):
print("Hello, " + name)
greet("Zavont")
greet("Developer")

To let a function return a value, use the return statement:

def multiply_by_five(x):
return 5 * x
print(multiply_by_five(3))
print(multiply_by_five(9))