Dave Drohan (SETU)

BACK NEXT

Writing your own methods (2)

In this step, you will work on reproducing the code example 5.6 from your lectures.

Example 5.6

Create a new Processing sketch in your workspace and call it Example_5_6.

Enter the following code into the sketchbook (don’t cut and paste, write it out):

int value = 30;

void setup()
{
    int result = timestwo(value);
    println(result);
}

int timestwo(int val) 
{ 
    val = val * 2; 
    return val; 
}

This code calls the timesTwo method from the setup() method. This means that the timesTwo method is only called once.

The timesTwo method takes in a parameter of type int, it multiplies the value of this parameter by 2 and then returns the value back to where the method was called from. It stores the returned value in another variable of type int that is called result. The value of the variable result is printed to the console.

Run your code. Does it work as you would expect? Is the value 60 printed to the console?