Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
8.11. Side Effects¶
We say that a function has a side effect if it makes a change in the program or output that is not reflected through the return value.
Side effects are sometimes desirable. For example, in turtle graphics, a function that causes a turtle to draw a square on the canvas may not return any value, but the side effect of running that function is that there is now a square on the canvas that the end user can see, and possibly, the turtle is in a different position after the function has executed.
Additionally, if you have a function that returns a value (such as an average), but also contains a print statement that prints out intermediate values to the console (such as the total used to calculate the average), that printed output is a side effect of calling the function. Both of these are good examples of positive and purposeful use of side effects.
Directly editing a global variable is another side effect, but it is one we typically want to avoid. For example, consider the code below:
In this example, the statement on line 2 says that we want to be able to access and edit the global variable y. On line 3, we change the value of that variable. While this works, it is considered bad form - it is harder to debug becuase it is less clear, when we print out the value of y on line 7, where the value of y was changed. It’s actually easy to figure it out in this very short code snippet, but it would be much more difficult if the double() function was 300 lines of code away from the print(y) statement.
The way to avoid using side effects to change global variables is to use return values instead: Pass the global variable’s value in as a parameter, and set that global variable to be equal to a value returned from the function. For example, the following is a better version of the code above.
In this example, it is obvious on line 6 that the value of y is being modified by calling the double() function.
To summarize, any lasting effect that occurs as a result of executing a function, that isn’t represented through its return value, is called a side effect. There are three ways to have side effects:
Printing out a value to the console. This doesn’t change any objects or variable values, but it does have a potential lasting effect outside the function execution, because a programmer can see the output and be influenced by it.
Changing the value of a global variable.
Changing the value of a mutable object (which we will talk about in the chapter on sequences).
8.11.1. Documenting Side Effects¶
When you create functions that have side effects, it is particularly important to document these as post-conditions. This may not be necessary when you are using print() statements inside a function to help debug things while you are in the middle of programming, as those are likely temporary statements that will be removed before you submit/share your code. But other side effects are really critical to document. Here is an example: