Functions
Function Definitions¶
Use Case¶
Extract code into a separate segment to reduce complexity, and allow for re-usability.
Functions are separate sections of code that can be executed at will. They have parameters that describe the input values into a function and are used to control a functions’ inner workings. Further, functions may generate an output value that can be returned to indicate for example a calculation result.
A function definition only declares which parameters and return values there are and what code will be executed. To actually run the code inside a function, it needs to be called.
When a function modifies the programs’ state directly (besides returning a value) it is said to have a side effect. Side effects are usually considered bad practise, since they can make programs very confusing and hard to understand and debug. Functions that rely only on their parameters for information input and have no side-effects are called pure functions.
Example: Function with no Parameters¶
This is a minimal example of a function that has
Input Parameters | Return Value | Side effects | Pure function |
---|---|---|---|
— | – | – | Yes |
# Function definition
def say_hello():
print("Hello World")
# Function call
say_hello() # No variable needed to catch return value
Example: Function with Parameters and Return Value¶
This is a more complex function that clamps a value between a minimum and maximum.
Input Parameters | Return Value | Side effects | Pure function |
---|---|---|---|
value, | clamped value | – | Yes |
minimum, | |||
maximum |
# Function definition
def clamp(value, minimum, maximum):
if value < minimum:
return minimum
elif value > maximum:
return maximum
else:
return value
# Function call (positional arguments)
clamped_value = clamp(10, 0, 5) # clamped_value will be 5
# Function call (named arguments)
clamped_value = clamp(minimum=0, maximum=5, value=10) # clamped_value will be 5
Function Call¶
Don’t forget the parenthesis
To call a function you write down its name followed by parenthesis (
and )
.
If there are arguments for the function they are specified between the parenthesis.
But even if there are no arguments, the parenthesis must be there.
Calling a function refers to its usage to execute a functions’ code according to the function definition.
Use case¶
Employ a previously defined function to complete a more complex task.
A function may require additional information to complete its task. When calling a function, these are referred to as arguments. Some arguments are required, others may be optional or use a default value instead.
Example 1¶
Call the function print(…)
without any arguments
You still need the parenthesis (
…)
even if they are empty!
Example 2¶
Call the function print(…)
with one argument.
Example 3¶
Call the function print(…)
with many arguments.
Multiple arguments are separated by a comma.
Example 4¶
You can also use variables as arguments.
Example 5¶
Arguments have names and may be referenced by them.
Here we use --
as the word separator; The corresponding argument is named sep
.
The names and meanings of the possible argument names are defined by the function definition.
You may want to check the functions’ documentation for details.
Output: