
Title:
0529 Identifiers And Storage Solution

Description:

Now let's work through it from the top.

Initially x is 1, and then I define a new function tricky, but I haven't called it yet,

so I'm not actually going to make any new variable spaces over here.

And then I print out the value of x.

Currently x is 1, and now I call tricky on x,

and we noted before that whenever we make a function call we get a new box

corresponding to that function call. This one knows who its parents are,

and this new box is going to have room for all the formal parameters in tricky,

also x, and this time we're passing in x.

The current value of x is 1, huh, so we've got almost a duplicate sort of state.

So now we're inside tricky, and we say x=x+5.

The real question is: Which one do we change?

The answer is this bottom, more specific one down here.

This x has shadowed, has taken the place of in our hearts, the old one.

So I change just this one to 6, so here, when I'm printing out the value of x,

we look in this world, we see a 6, and we print it out.

And now we return from tricky, and when we return from a function call,

you can imagine we throw away all of its space, and we're back here in the normal world.

Well, what's the value of x here? It's 1.

So this assignment statement, x=x+5, changed the function's actual argument

but did not change this more global value of x.

We call these boxes that help us keep track of storage places for variables environments.

Kind of like before, when we were talking about state being a bit like the world,

you can view a particular environment like Central Asia or Western Europe or Antarctica

as being a little region in the world that might have its own values for variables.

If I want to know what the temperature is, well, it depends on the current environment.

Are you in the Sahara desert or are you in Northern Sweden?