Okay, hello, today's topic.. Well not today. Whatever this... this...
What do you watch? Like one video a day? Come on they're like seven minutes long. This seven minute period's topic is....
Variable Scope. Now...
This is admittedly a kind of a strange topic to just sort of insert in here
Because we did have a whole set of videos about variables. You know variables. Define a variable.
What's a variable, datatypes all sorts of things. I think we made those videos. Yes, anyway...
I'll make them if I have it.
But what are we doing when talking about variable scope here shouldn't that have gone back there?
In truth this idea of variable scope is probably a concept that goes with variables. At the same time
we've never really had a need to concern ourselves with variable scope.
But this moment of using a loop is the first time where it makes some sense.
It's only gonna make more and more and more sense as we get further and further along with many more programs. Okay, so...
The people just keep talking always. It's very distracting. Quiet! Settle down out there! OK, oops.
Now they're gonna start begging back anyway
Okay, so let's think about variable scope just to recenter ourselves. Let's write the words variable scope. Now...
We're used to writing Processing sketches that have setup() and then have draw() and we said okay
well, you're gonna use variables in your programs.
What if we needed to put... we needed a variable, x for example. We need a x called...
that's of type float. It's gonna be initialized at 0. We always put our variables at the top of the code or
so we thought but I'm sure you've seen examples or maybe by accident
you've actually put your variables somewhere else. What we're talking about with variable scope is what happens
when we start to declare our variables not use them, by declare
I mean give them a name and a type somewhere else in our code. What happens if we put them in setup()?
What happens if we declare them and draw()? What happens if we have a conditional statement and we declare a variable inside a conditional statement?
What does that mean? Well conceptually we have these two these concepts. This is what's commonly referred to as a global variable.
Now is anything really global or local isn't global just a larger local?
I mean, that's what I think.
But we can separate the idea here that this is a global variable meaning if it's declared at the top or outside of setup() and
draw() truly, then we can use it anywhere in our program. Anywhere at all, it's global.
We can say x whenever we want and it's this variable x.
If we happen to declare a variable somewhere else it is only usable in that block of code.
Let's go. Let's take a look at what I mean by this, okay?
So I actually have an example prepared that had two that
I was going to use for variable scope, but I'm actually just going to open up a sketch that we did
In with conditionals. Bouncing ball. Let's open up this bouncing ball sketch and let's run it.
So we can see this bouncing ball sketch has two variables circleX. It has circleX
and xspeed. If I'm standing here, I mean yeah you can see. Not that you need to see me.
Whatever it has circleX, it has xspeed. Two variables. Global variables. We're using them and setup().
We're using them in draw(). All over our code. What would happen if by accident we weren't really paying attention one day and
instead we wrote our code like this:
Right? We have circleX equal. We say okay. What's happening at the beginning of our program?
We say we want to use a variable called circleX = 0 and xspeed which is 3 and then we're going to use
those variables later in our program if I run this code...
It says down here at the bottom cannot find anything named circleX.
That is the error message you get when you're
trying to use a variable that you've never declared your intention to use in the first place.
But is that wrong didn't we declare our intention to use it right here in setup()? float circleX. The problem is...
A variable that is declared... eraser... a variable that is declared inside setup()...
Float, if we have some variable, float x here inside setup()
This is referred to as local. Meaning it is local only to the setup() function.
It's the rest of the program knows nothing about it
So here we could use that variable anywhere we want in setup()
But we can't use it in draw()
because it doesn't exist and draw only exists in setup(). What is the scope? You have to look and say okay...
Where are my curly brackets?
What set of curly brackets am I in that wherever I've I'm declaring inside a set of curly brackets
that's the only place I can use that variable. That goes for a conditional statement a loop, other types of curly braket things
we're gonna see later like functions and objects. It's a local variable, local only to that function.
So now we have to ask ourselves this question...
That's confusing... Why not just make everything global?
Then I don't have to worry about whether or not
I'm supposed to use it or not. Because I could just use it wherever I want.
And you know what I think you're right?
Just make everything global and your life is easier. Certainly that's where we are now. We're writing small programs. They have a few variables
We're using the variables and setup
We're using them and draw everything should just be global
But there comes a time where that convenience can be a bit of a mess. What if you had a
thousand variables in your code? You might actually someday have a thousand variables in your code.
You don't want to have a thousand global variables at the top?
It's gonna be very confusing. Things aren't gonna get put into packages in different places
we're gonna learn about objects and classes and tabs and functions a
loop is sort of a place where we might actually have an instance for a local variable that makes sense because if we don't need
It everywhere why declare it as global when we could only give ourselves some problems?
okay, so...
I'm moving over here now
I don't know why I feel it is helpful if I announce that. I also I really wanted to try this which is the go
underneath just
Pop up and appear. I don't know what that does that probably does nothing, okay?
So let's go away from this programs fix this program by putting our variables
back to the top
And running it
Our bouncing ball works again, and now let's look at this
Let's look at this sketch, so this sketch, and I'm at six minutes
We're gonna be done in a couple minutes this sketch is drawing a set of lines horizontal. You seen up to these now
so
Where is our variable here? Well? This is a static sketch?
it doesn't have setup and draw and you notice I declared in two x equals zero down there because it kind of goes with the
Loop x should start at zero and then increment by 20 and we should draw those lines
I mean, I could have put int x up here
and
We're gonna get the same exact result
Because it's just a static program, but now let's let's go ahead and put a set up in here
And put draw around all of our drawing code. So now it is a dynamic program
It's to do the same thing every time through draw and we run it and oh
Crap it's not doing what we wanted to do why?
because for this loop to work x always has to start at zero where x is only is zero once then it goes up we've
got to make sure every time through draw we reset Ex back equal to zero so
We just add this line of code and now our program works again
But here's the thing did we use x and set up no did we use it anywhere else?
Do we use it in mouse press key pressed is there some other code? No, so it's confusing
Why is it up there? If we only need it just for this loop really and so I think it would make more sense
For us to actually just declare x the moment because we only need it down in this block of code
So this is where it makes some sense
to use a local variable because we only need it here and our code is a little bit cleaner to read and see and we
Can't by accident use it somewhere where we don't need it or want to use it?
So that's what variable scope is for we have variables that are accessible and usable in certain parts of the code
Certainly there are questions of efficiency like the computer can release the memory when it's done with them
But you know I don't care about that. We're writing these little skipping. You know you can worry about that later
Some time where you have a big giant complicated program, whatever. I don't even know why I mentioned that oh
Ok that's it. That's variable scope. I don't even think an exercise really needs to go with this one
I suppose if I were to give you an exercise
It's go back look at something you made in Processing that has a lot of global variables at the top
Any of those variables do you need them everywhere?
Can you make some of them local at least maybe try some loops and by the way...
When you're using a for loop
You if we turn that turn that into a four loop with a local variable and see what happens. There is your exercise, okay?
I know how to juggle, but I'm not going to do that
Someday, I will
Hopefully this video will be cut off before I talk about... what am I talking about
Okay, that's it end of this video. We're doing well today. This is good. This is good. Okay clap. Goodbye