Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In the last movie, I gave you an overview of how to use variables within Processing. Again it's a great thing for saving time in coding for making use of information over and over again. And it's an essential practice for data visualization. In this movie, I want to give some more information about creating variables. The first thing I want to talk about is what's called the scope of a variable, and that is in Processing, a variable is either a local variable or it's a global variable. And I will demonstrate that by getting down to a little example here.
First I'm going to call this one, put its name in there as a comment. Now what I'm going to do it I am going to create something what's called global variables. A global variable is a variable that can be use anywhere in a sketch, even if you have got a million lines of code, that variable can be used anywhere. A local variable is one that has a constrained scope. It can only be used in part of a sketch. For instance, within a blog of code or within a loop. And I'll give you examples of all of those. Right now I'm going to create a few global variables.
What I'm going to do is I'm going to create a variable x int x = 0; so there's one global variable, it will work anywhere in the sketch. I will do another one, y = 50; it's common to put the global variables at the top, you actually have to put them up before you use them. But I also like to separate them and make them easier to find. Now I'm going to get into something that we're going to do it little bit later and that is blocks of code for what are called dynamic drawings.
You don't need to worry about this too much, but what I'm doing is I am creating a separate blocks of code right now. And a lot of drawings, what you have as a setup block, I'll explain what all of that means later. Now I'm going to put my window information in here. I'm going to make the window (600, 200) which we've done before, turn on the anti-aliasing. Then what I'm going to do is I'm going to create a local variable.
And that is a variable that only exists within this particular block of code. I'm going to make it a color variable. And that is a darkGray. Once I've created this variable, I can use it by saying, background and then I'm going to put the name of the variable, right here, darkGray and now if I hit Run, you can see that my display window is 600 pixels wide 200 pixel tall and it is in fact a dark gray in background.
Then, what I'm going to do is I'm going to just put a print line function, always nice to check what's going on. And what I'm doing here is a repeat of what it is in the last movie. I'm going to ask it to say darkGray and then give the variable because it's a hex code, I actually need to put it within this little hex thing and then with a comment and then say, I only want six characters in it. I am also putting the hash tag in the comments because the Processing command doesn't include that one by default.
So if I run this now. I get my window and at the bottom, you can see it says darkGray=333333. So that's working just fine. Let me say something about how I name variables and I've talked about this before but just to be clear. A variable name has to be one word, and there can be no spaces in it and it has to start with a letter. Also, it's standard practice for variables to be lowercase. Although if you want to concatenate words like darkGray, what you do to use what's called BumpyCaps or CamelCaps or medial capitalization.
And you simply capitalize the word as it comes through. Part of this is because words that start with capital letters are reserved for classes which we're going to deal with later. Also, there's no punctuation allowed in a name with the exception of an underscore (_). You can put underscores and things if you want. Also, there are built-in or system variables. For instance, the word width is a recognized variable in Processing and there is other ones. And when you use them they'll be colored by Processing, you can declare your own variable on top of those.
But you need to know that your variable will then replace the system function. And that's usually a bad idea but it can be done. What I'm going to do now is I'm going to create another variable called randomFloat. And one of the neat things that I'm going to show you a little bit later is that you can get random numbers from Processing. randomFloat1 equals, now this is the first one, it'll be different every time we run it.
You see right now it says 9.29, if I run the sketch again, I should get a different value 1.23, run the sketch again. And you see how it changes each time. That's one of the neat things, it's a variable. And it takes a different value every time. Now what I'm going to do is, I'm going to show that there is a separate block of code, I'm going to come down and create another block of code, the draw block is extremely common you'll see. In fact, most sketches will have the set up block and the draw block.
I can define the background color again in this block if I want to. And you watch what will happen. It freaks out because even though, I have a variable named darkGray in the previous block, that was a local variable, and that variable has not been defined in this block and so it wants to know what's going on. So what I can do is I can simply create the variable darkGray over again, but I'm going to make it slightly different this time. I'm going to make it really dark.
And now you will see what happens is that the darkGray variable from this one overrides the earlier one. See it's practically black this time around. And so these are some general pointers on how to work with variables. Again, information that you need to declare the variable type, you need to give it a name, you do need to a usually initialize it. One trick that you can do sometimes is you can declare a variable globally, like this, without initializing it. And then what you can do is, you can save the initialization until you get to a later block of code.
For instance here, I can now say z=25. And that gives you a little bit of flexibility in how you deal with things, especially if you need a variable to be accessible in more than one block of code but you need it to be able to change as you go through. And so as those are some general pointers on dealing with variables. In the next movie, we will talk about modifying each of those.
Get unlimited access to all courses for just $25/month.Become a member