Functions are ways of doing things in Python. They take some input and do something with it, and can return a value. For example, a function can return the results of a calculation. If your function takes an argument, it will go inside the parentheses.
Functions are first defined then called to be executed. This allows you to first write the function but only call it (execute its code) when you need to in Python.
Example:
def multiply(a, b):
result = a * b
return result
multiply(2, 3)
The first code block defines the function multiply()
which takes a
& b
as arguments. The function sets result
to the caluclation of a
x b
. It then returns the value of result. Finally, the function is called with arguments 2
& 3
, which will be substituted for a
and b
in the function definition.
- More in-depth exploration of functions on Programiz.
- Simple walk-through of functions on w3schools.