 G'day all my Robert's here. Let's talk about scope. So we've got a very simple example of a Processing program that uses scope exercises it in some way. We don't need this down here But most and we don't need the console here. Just maybe be able to get rid of it in case not it all fits on the screen So that's good I've got the debugger open on the left here, but ignore it for now because we're in the middle That was in the middle of the last execution This is a program that wants to actually let's turn the debugger off This is a program that wants to animate a shape across the screen and animate a shadow of the same shape appearing down on the bottom of the screen There are many ways to do that and this program just happens to do it this way it does it by introducing a Two other functions one that'll draw the UFO at a position and one that'll draw the reflection at a position and the UFO one fills it with the kind of magenta color and then draws in the lips up the top of the screen and the Reflection fills with a black color and draws it down the bottom of the screen Interesting thing is both these functions need to know where they're going to draw that Shape so they both take a single parameter telling it the X position of the shape But also because we're animating that Position across both of them. We need a global variable representing the X position of the shapes both shapes So that X position up here Is a variable getting incremented so that animates across the screen and we're using it to draw the UFO And we're using it to draw the reflection The particular characteristic of this program is that this variable name here X pause is the same as the variable names here and here What we want to understand is that every time you see something that looks like a variable declaration Int pos X that's definitely a variable declaration Here's another one in pos X looks just like a variable declaration and yet another one That yes, every one of these does cause a variable to be created and a slot in memory to be set aside for that variable Even though these are here in the formal parameter position. They are in fact exactly the same as variable declarations So we're going to try and have a bit of a look inside the memory while it's running via the debugger Trying to show you what I mean So the debugger set with a breakpoint here on the first call to background which is why it stopped before it's drawn anything and You can see that there's a single variable called X pos that has a value of zero That's exactly what I expect because I declared a variable of X pos up here the codes run through this section It's run through this section. So it's set it to zero and we're here Now if I step Just the one step. I'm now down to the call to UFO at and That so nothing's been drawn yet or the background might have been drawn, but it looks like it's struggling Actually, nothing will get drawn on the screen until a full draw loop has run So this is the UFO at call and we're passing in as an actual parameter that X pos which will be the value zero Now that's going to cause the code to jump down here to this function And run this Function definition and you can see in the function definition is a declaration So as soon as processing does that it gets past that declaration Which means it creates another slot in memory and it's done that up here So there are now two slots in memory both called X pos And the debug has been nice to us here because it's added this little magic this on the front to help I see that one of them is The new one versus the old one, but we can't really rely on that That's a kind of internals of processing thing in reality and from our point of view. We've got two entirely separate variables both called X pos and That creates potentially a problem because in this line here the very next line of the code We say please look up the memory slot X pos and find out what's in there well which X pos There are two of them and the scoping rules will tell us which X pos The other thing to realize about scoping which we'll see in this example is that if I step again It's going to go through the rest of the UFO app and back out into draw So the ellipse call gets called Step again, and we're back out of that UFO call and one of the X pos is went away Just gone So that's also a consequence of the scoping rules We'll be talking about the fact that variables have a life and that life Is finite so one of those X pos is that we created has gone away again And what we'll see if we keep tracing is that when we get the reflection at it introduces yet another X pos Because we've gone through another declaration step and again It's got a little trick that we can use to work out which is which but internal details So we can't rely on it And again that one puff disappears when we come back out The other one's hanging around and we're getting a drawing happening But this question of when I've got two variables that have the same name Two slots in memory that have the same name and Then I try and access a Memory slot with that name which memory slot will I get? We ask our scoping rules and our scoping rules tell us so I guess we better find out scoping rules work You