Functions with side effects
At this point in the course, you have thoroughly been introduced to functions. However, all functions we have taken a look at so far have at least one parameter and always return a value. In this chapter we would like to introduce you to functions that don't have any parameters at all or don't return a value. When we initially introduced functions, we drew the comparison to mathematical functions. In maths, every function has at least one input (parameter) and always returns a value. It makes little sense to define a mathematical function that doesn’t take any inputs or return a value, since calculating results from a set of inputs is their entire purpose. Therefore, it may sound strange to introduce functions in programming that don't require inputs or produce an output.
Let's start with a simple example of a function that takes a Boolean as input and also returns a Boolean. The function toggle_light_switch will invert any boolean value passed to it and return the result. The variable light_on defined in the global scope represents a light switch.
Output:
Whenever we want to use the function to toggle the light switch, we have to pass the current value in the variable light_on to the function and reassign the return value back to it. We can simplify the example by accessing the counter-variable defined in the global scope from inside the function directly instead of passing it in.
Output:
As you can see, the function does not take any parameters, which is symbolized by the empty set of parentheses in the function definition as well as the call. However, we can still simplify the function further by directly modifying light_on from inside the function instead of having to assign the return value back to the variable.
Output:
In this version of the example, the function does not have a return type anymore, which can simply be omitted in the definition. Using toggle_light_switch is much easier now since we don't have to remember to assign its return value back to the light_on variable. We also don't have to pass the current state of the variable as an argument anymore. These changes are especially handy when we want to call toggle_light_switch multiple times in a row.
You might have noticed that we also don't have to use a return statement anymore since we are not returning a value from the function. However, if you would like, you can still use an empty return statement to mark the end of the function.
Dangers of using functions with side effects
As we discovered, defining a function with side effects can be handy. However, working with such functions poses significant risks. This happens because you are making your function rely on variables that are external to the function. This can be risky because the external variable may change in a way that is unexpected to the function. Also, you might want to use your function in another program, which becomes harder since you have to copy all the external variables along with it as well.
Therefore, we want to encourage you to write pure functions wherever possible. This way you can avoid problems that are hard to diagnose and allow your functions to be reusable in different places without having to rely on external variables.
However, functions with side effects do play an important rule in allowing us to accept input from the user and write text to the screen, which we will take a look at in the following chapter.