Functions are like a recipe

Now that we’ve learned about variables, and how to control the flow of a simple program with if-statements, we can start to use functions.

This version of Pacman is drawn by a function.


That means it’s very easy to repeat the function several times and draw Pacman over and over again.


What is a function?

We use functions to put several commands into one ‘box’ so that we can use it and reuse it as often as we like. This means we just remember the name of the function and a few other details without having to remember all the code which makes it do what it does.

When we use a function we say that we are “calling” a function.

Functions are like a recipe that you use often, although sometimes you want to change a couple of ingredients. We tell the function which ingredients to use by  sending through a couple of  variables when we call the function. We call these special variables  ‘parameters’.

We’ve already been using functions, for example to draw an ellipse or a rectangle. We’ve also been using and changing parameters – these are the x and y or width and height values.


You can tell that something is a function if its name is followed by those round  brackets (). Anything inside the brackets is a parameter.

Defining functions

Before you can call a function you have to define it. This means you have to provide the list of commands which will execute when you call the function. A Processing function should include the following:

  1. The variable type which the function returns e.g. int, float, String, Boolean etc. For now, you will start your functions with the word ‘void’. This means we don’t need it to return any variable values.
  2. The name of the function
  3. The name should be followed by round brackets ( ) These are also called parentheses or braces and they contain the parameters functionname(parameter1,parameter2).
  4. A block of commands enclosed in curly brackets { … commands go here }. You’ve already been learning how to write commands, so they should be pretty easy. Just remember to end them with a semicolon ;

void setup() and void draw()

There are two important functions we need to know about to start doing cool things in Processing. These are the setup() and draw() functions.

You can see they are functions because they have round brackets. They don’t have any parameters, the brackets are empty. Both of these functions start with the word ‘void’. Don’t worry about this too much, the word ‘void’ means that they don’t return anything to our program.

The setup() function only runs once, to set up what is needed for the sketch. The draw() function runs many times per second – too many times for our eyes to see them separately. This is how we can create animations and interesting interactions with Processing.

You can see setup() and draw() at work in the following example if you paste it into Processing and press Run. You can also  see it at work in this sketchpad. or copy the code from here:

int x=10;
int y=10;ch
void setup(){
void draw(){
    //remove the comment markers on the background colour to see the animation
      fill(255); //creates white colour
    ellipse(x,y, 50,50);
 //can you write an 'if' statement to send the ellipse back to its 
 //starting point after it has crossed the screen?

3 thoughts on “Functions are like a recipe

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s