Start learning with our library of video tutorials taught by experts. Get started

Interactive Data Visualization with Processing
Illustration by

Understanding variable scope


From:

Interactive Data Visualization with Processing

with Barton Poulson

Video: Understanding variable scope

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.
Expand all | Collapse all
  1. 3m 16s
    1. Welcome
      58s
    2. What you should know
      1m 22s
    3. Using the exercise files
      56s
  2. 11m 51s
    1. Overview of data visualization
      11m 51s
  3. 11m 53s
    1. Installing Processing
      3m 38s
    2. Overview of Processing
      4m 5s
    3. Exploring libraries
      4m 10s
  4. 1h 1m
    1. Basic setup
      7m 31s
    2. Drawing points
      4m 37s
    3. Drawing lines
      5m 6s
    4. Drawing ellipses and circles
      5m 24s
    5. Drawing arcs
      6m 54s
    6. Drawing rectangles and squares
      4m 58s
    7. Drawing quadrangles
      3m 25s
    8. Drawing triangles
      2m 55s
    9. Drawing polygons
      3m 37s
    10. Drawing simple curves
      4m 54s
    11. Drawing complex curves
      6m 46s
    12. Drawing Bézier curves
      5m 38s
  5. 54m 3s
    1. Introduction to variables
      10m 44s
    2. Understanding variable scope
      6m 53s
    3. Modifying variables
      9m 8s
    4. Creating arrays
      9m 53s
    5. Modifying arrays
      6m 37s
    6. Creating strings
      7m 3s
    7. Modifying strings
      3m 45s
  6. 1h 2m
    1. Incorporating randomness
      7m 59s
    2. Using Perlin noise
      4m 24s
    3. Shuffling with Java
      3m 30s
    4. Specifying line attributes
      8m 2s
    5. Changing placement modes
      5m 45s
    6. Understanding color attributes and functions
      4m 16s
    7. Exploring color spaces
      7m 44s
    8. Using color palettes
      7m 5s
    9. Transforming the grid
      8m 38s
    10. Exploring the attribute matrix
      5m 33s
  7. 52m 7s
    1. Building code blocks
      5m 57s
    2. Writing a while loop
      3m 52s
    3. Using for loops
      5m 35s
    4. Creating conditionals
      14m 50s
    5. Working with easing
      10m 51s
    6. Creating spirals
      11m 2s
  8. 18m 55s
    1. Mouse tracking
      3m 54s
    2. Hovering and clicking
      11m 16s
    3. Understanding keyboard interaction
      3m 45s
  9. 27m 32s
    1. Specifying fonts
      6m 43s
    2. Using images
      5m 51s
    3. Playing a video loop
      6m 20s
    4. Exporting video
      3m 47s
    5. Adding sound
      4m 51s
  10. 20m 49s
    1. Creating functions
      11m 48s
    2. Creating classes and objects
      9m 1s
  11. 31m 10s
    1. Using embedded data
      5m 26s
    2. Working with appended text data
      6m 4s
    3. Working with appended tabular data
      10m 26s
    4. Reading XML data
      9m 14s
  12. 48m 17s
    1. Generating dot plots
      11m 11s
    2. Building scatter plots
      10m 0s
    3. Making line plots
      9m 55s
    4. Creating bar charts
      9m 12s
    5. Checking out examples of maps, hierarchies, and networks
      7m 59s
  13. 20m 57s
    1. Introducing some principles of 2D design
      13m 44s
    2. Understanding color theory
      7m 13s
  14. 24m 46s
    1. Interacting with zooming, rotating, and sliding
      6m 26s
    2. Implementing slicing
      6m 47s
    3. Using rollovers
      5m 58s
    4. Introducing the GUI libraries
      5m 35s
  15. 10m 35s
    1. Sharing via OpenProcessing and other sites
      3m 19s
    2. Saving as a desktop application
      2m 42s
    3. Saving as JavaScript
      1m 47s
    4. Saving as an Android application
      2m 47s
  16. 2m 38s
    1. Where to go from here
      2m 38s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
Interactive Data Visualization with Processing
7h 43m Beginner Sep 25, 2012

Viewers: in countries Watching now:

Start communicating ideas and diagramming data in a more interactive way. In this course, author Barton Poulson shows how to read, map, and illustrate data with Processing, an open-source drawing and development environment. On top of a solid introduction to Processing itself, this course investigates methods for obtaining and preparing data, designing for data visualization, and building an interactive experience out of a design. When your visualization is complete, explore the options for sharing your work, whether uploading it to specialized websites, embedding the visualizations in your own web pages, or even creating a desktop or Android app for your work.

Topics include:
  • Exploring the need for creative data visualization
  • Drawing basic lines and shapes
  • Introducing variables, strings, and arrays
  • Modifying drawing attributes such as color
  • Making drawings more dynamic with animation loops and spirals
  • Creating keyboard- and mouse-based interactions
  • Adding images, video, and sound
  • Reading in text or XML data
  • Creating plots and charts
  • Publishing and sharing your work
Subjects:
Developer Programming Languages
Software:
Processing
Author:
Barton Poulson

Understanding variable scope

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.

There are currently no FAQs about Interactive Data Visualization with Processing.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Interactive Data Visualization with Processing.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Notes cannot be added for locked videos.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.