variables

Variables lie at the heart of programming. They are used to read and write data by providing named access to data stored in memory. Using variables allows us to do many things, among them:

Variables consist of a name and a type.

name / identifier

A label consisting of some subset of the character set used by the programming language. The name cannot already have a meaning in the language itself; these are called reserved words. The Processing reserved words can be found in the Reference section.

In Java (and by extension, Processing), the following naming rules apply:

Valid names include foo, foo_bar, f00, _foo, xposition

Invalid names include 35foo, $bar, 245, (, true, int

The Java naming convention suggests camelCasing variable names, with a lowercase first letter. It is a good idea to follow this convention as it makes programs easier to read and variables easily identifiable at a quick glance of the code.

type

The kind of data stored in the variable. A type constitutes a range of values and valid operations on those values.

A number of other types exist, which we will discuss later on.

using variables

A variable must first be declared, meaning we tell the program to reserve some space in memory for it. The syntax for declaring a variable is type variableName;

int temperature;
int distance;
float radius;

It is good programming practice to also initialize the variable either at declaration or immediately after declaring it. You initialize variables by using the assignment operator =:

int temperature = 75;
int distance = 34928;
float radius = 3.45689;

A variable can only be declared and initialized once in a program. A value can be assigned to it over and over. Note that initializing just means assigning for the first time.

// declare the temperature variable
int temperature;
// initialize it
temperature = 75;
// assign a new value to it
temperature = 85;

Note also the the semicolon ; at the end of each line. This tells the compiler that it has reached the end of a statement. You must use the semicolon to terminate your statements.

Finally, variables can be read. This is done by simply using a variable wherever you would normally use a value.

// declare the temperature variable
int temperature;
// initialize it
temperature = 75;
// assign a new value to it
temperature = 85;
// print the value to the console
println(temperature);

variables for modularity

Let's build a sketch that draws a dot in the middle of the display.

// set the size of the display
size(200, 200);

// set the background color to white
background(255);

// draw a point in the middle
point(100, 100);

We will now modify the last sketch, by drawing three dots spaced 20 pixels apart along the horizontal axis, with the middle dot in the middle of the screen.

Specify your display as above, then draw the points:

// set the size of the display
size(200, 200);

// set the background color to white
background(255);

// draw three points along the horizontal axis
point(80, 100);
point(100, 100);
point(120, 100);

This code works, but it's a bit cumbersome. For instance, if we want to change the offset to 10 pixels instead of 20, we have to alter the code in three different places. To address this inefficiency we will generalize the solution, meaning that we will rewrite it to be more generic and thus applicable to a greater number of potential problems.

We start generalizing by using variables wherever we can. The easiest thing to do in this case is to create a variable that represents the spacing in-between the three points.

// set the size of the display
size(200, 200);

// set the background color to white
background(255);

// draw three points along the horizontal axis
int spacing = 20;
point(100-spacing, 100);
point(100, 100);
point(100+spacing, 100);

Now we can get a significant change in the display with a small change in the code, for instance, by making spacing = 35.

Another strategy for generalizing is to look for repeated literals (literals are constant values as opposed to variables). In our example, the repetition of 100 suggests that it is a candidate for generalization:

// set the size of the display
size(200, 200);

// set the background color to white
background(255);

// draw three points along the horizontal axis
int spacing = 20;
int xPos = 100;
int yPos = 100;
point(xPos-spacing, yPos);
point(xPos, yPos);
point(xPos+spacing, yPos);

built-in variables

Processing has some built-in dynamic variables that are quite useful for making your code more modular. Since they are built-in, these variables should not be declared, initialized or assigned; they should just be read.

We can modify our three dots example so that it works for any size window, without having to change any of the code.

// set the size of the display
size(200, 200);

// set the background color to white
background(255);

// draw three points along the horizontal axis
int spacing = 20;
int xPos = width/2;
int yPos = height/2;
point(xPos-spacing, yPos);
point(xPos, yPos);
point(xPos+spacing, yPos);

Processing Workshop

Elie Zananiri
Alberta College of Art + Design
3-5 April 2008