An overview of variables in processing, and how they are used

A variable is something, that can take on different values at different times. In order to work with variables, we have to be familiar with different concepts.


Declaring a variable

In order for us to use a variable though, we first have to declare it. For example:

int x

And voilà, we declared the variable int as x.


Initialising a variable

In the second step, we will initialise the variable by setting a value to it. For example:

int x = 10

The variable x is now set to 10, and we can call x throughout our sketch, and modify it.


Types of variables

In Processing there are multiple types variables that we can use in a sketch.


1. int variableS

The int variable is used for integers, hence its name. Integers are whole numbers, such as 1, 2, 3, or 4. For example

int n = 100


2. Float variableS

The float, or floating points variable allows us to use values with decimals. For example

float e = 2.71828;


3. Boolean variableS

A boolean variable is a variable which is true or false. In Processing boolean variables are implied, i.e. they don’t need to be declared. For example

boolean switchVar = true;

Additional things we can do with boolean operators is inverting them by pre-pending an exclamation point prior to it. The exclamation point is a “not” operator. For example:

switchVar = !switchVar;


4. Char variables

Char stands for character. A character variable has a single letter or a number and it must be in single quotes. For example:

char charVar = 'V';


5. Byte variable

The byte variable is interesting in that sense that it is used in communication with serial ports such as Arduino. The byte variable has a range from -128 to 127.

byte dozen = 12;


6. Color variable

We can use the colour variable in such a way that we don’t have to remember the hex codes throughout our sketches, but instead refer to a colour’s name. For example:

color cherryBlossomPink = #FFB7C5;


Scope of a variable

A variable has always a scope in that sense that it is either global or local. Global will make the variable available document wide, whilst local, i..e in a block of code, will make the variable available in precisely the block of code that it is defined in.

A local variable can override one that was defined in a previous code block.


A tip:

We can conveniently use a global variable, that has not been initialized in the beginning of a document and then initialize it instead in the local block.

Global variables have a constraint.


Naming conventions

  • Variables should be Lower case, but when concatenating it is best to use camelCase.
  • A variable has to be written in one word, and has to start with a letter
  • Words that start with capital letters are reserved for classes.
  • It is not allowed to put punctuation marks in variable names, with the exception of underscores.
  • The term “width” is a recognised variable in processing, among some others such as height. When using them they will be coloured in the built-in syntax highlighting. We can declare our own on top of those, and our own variable will replace the system function, which is a bad idea in general and not recommended.