Welcome back. There's one more tricky thing
that we want you to be able to reason about with functions,
it's called side effects.
If a function makes a change to a mutable object like a list or a dictionary,
that's called a side effect.
The vocabulary here is that the main effect of a function is the value it returns,
and any other lasting impact that it has is a side effect.
One other thing that I'll refer to as
a side effect is printing something out in the Output window.
Let's see a side effect of mutating a list.
First, this code gives us a little reminder that variables are local.
So, we create these two functions.
We make a local variable y,
that's in the global frame.
Then we invoke the double function.
The double function takes as input some value,
which it assigns to a formal parameter y.
So, notice that, we have a different y.
They happened to have the same value,
but they are completely different variables here.
Then we're going to assign on line two of the code,
a new value for y.
Watch what happens in the double-stack frame.
It's not going to affect what happens in the global frame.
So, when we execute line two in the double frame,
y now has the value 10.
That did not have any effect in the global frame.
You might also have noticed that the double function doesn't have a return statement,
and therefore, it returns the value none.
It doesn't actually matter in this case,
because we don't do anything with the value of double,
we're not assigning it to anything.
When we do finally get to line 10 and we're going to print out the value y.
We get the value y from the global frame.
So, it's the value five that's going to print out,
that the 10 that we had in the local frame for the double function.
Sure enough, we get the value five.
That was just a reminder that we have local variables,
and changing a local variable doesn't affect the global variable.
But our next lesson is that even though we don't affect a global variable,
we might affect a value that is shared by a local variable and a global variable.
So, variables are local,
but objects are not.
Here we're going to see that we mutate an object inside a function and it stays mutated.
On line number 12, we've created a list with four elements, our students.
On line 12, we have created a list called mylst.
Its value is this list of four strings,
"Our students are awesome," that's you.
Then we're calling change it.
On line 13, we call change it.
That creates a new stack frame for the change it function,
and its formal parameter,
LST gets a value.
The value it gets is whatever mylst had as its value.
Well, mylst was pointing to that list object.
So, the LST variable in the change its stack frame,
that LST variable also points to the same list.
This is important because,
now when we get to line number five,
list square bracket zero gets a different value.
Instead of being hour it becomes Michigan,
and instead of students we get wolverines.
So, notice that, mylst and lst,
these are still two different variables but because they're aliases for the same list,
when we finish this execution and then we print mylst,
mylst has the mutated value.
The variable mylst is pointing to the object which has been mutated,
and we get, "Michigan wolverines are awesome," instead of,
"Our students are awesome."
So, we call this a side effect.
The change that function is having a side effect on the list object.
Just as we talked about earlier when we first introduced
the idea of multiple aliases for the same list object,
this can get confusing if you're not careful.
Sometimes it's clear that a function is going to have side effects and you expect it,
but sometimes you'll be surprised and debugging can get difficult.
What happened to mylst? You'll ask.
To avoid potential confusion,
it's best to just avoid side effects in your functions whenever you can.
If functions never ever have side effects,
that's a style called functional programming.
There are programming languages built around that principle
of functional programming but Python is more flexible,
and we will sometimes make use of side effects but you should do
it sparingly and consciously. See you next time.