English subtitles

← 05-29 Identifiers And Storage Solution

dummy description

Get Embed Code
3 Languages

Showing Revision 1 created 05/13/2012 by Amara Bot.

  1. Now let's work through it from the top.
  2. Initially x is 1, and then I define a new function tricky, but I haven't called it yet,
  3. so I'm not actually going to make any new variable spaces over here.
  4. And then I print out the value of x.
  5. Currently x is 1, and now I call tricky on x,
  6. and we noted before that whenever we make a function call we get a new box
  7. corresponding to that function call. This one knows who its parents are,
  8. and this new box is going to have room for all the formal parameters in tricky,
  9. also x, and this time we're passing in x.
  10. The current value of x is 1, huh, so we've got almost a duplicate sort of state.
  11. So now we're inside tricky, and we say x=x+5.
  12. The real question is: Which one do we change?
  13. The answer is this bottom, more specific one down here.
  14. This x has shadowed, has taken the place of in our hearts, the old one.
  15. So I change just this one to 6, so here, when I'm printing out the value of x,
  16. we look in this world, we see a 6, and we print it out.
  17. And now we return from tricky, and when we return from a function call,
  18. you can imagine we throw away all of its space, and we're back here in the normal world.
  19. Well, what's the value of x here? It's 1.
  20. So this assignment statement, x=x+5, changed the function's actual argument
  21. but did not change this more global value of x.
  22. We call these boxes that help us keep track of storage places for variables environments.
  23. Kind of like before, when we were talking about state being a bit like the world,
  24. you can view a particular environment like Central Asia or Western Europe or Antarctica
  25. as being a little region in the world that might have its own values for variables.
  26. If I want to know what the temperature is, well, it depends on the current environment.
  27. Are you in the Sahara desert or are you in Northern Sweden?