Art and science have their meeting point in method. [Edward G. Bulwer-Lytton]
In addition to setup and draw Processing allows us to define our own methods. This provides two important features:
Methods are composed of a few parts:
We tell a method to execute it's code by calling it. Calling methods is a simple one line statement.
drawStar(); calls the method named drawStar
Can you see why all methods must have a unique name?
We can add some variables to our method so when we make a method call we can specify certain parameters of how the method should behave. There are two parts:
In order to use the parameters of a method, we must call the method with the same amount and types of data.
The values you put in a method call are referred to as arguments. The arguments must always match the parameters of
Draw star method with parameters:
void drawStar(int x, int y, float startScale);
Calling draw star with arguments:
drawStar(width/2, height/2, 0.2);
Here is an example of the drawStar method with parameters:
Every method will return to where the method was called once it has finished executing it's code. This means that the last line of code in the method body has been executed.
There is something else methods can do however, they can return a value. A method that returns nothing specifies a return type of void. Here are some examples:
int add2ints(int x, int y); will return an int
float add2floats(float x, float y); will return a float
boolean lightIsOn(); will return a boolean
apples getBagOfApples(); will return an array of apple objects
When a method specifies the type of value it will return, it must return that value somewhere in the method body. This is accomplished by using the special keyword return.
return x + y; returns the sum of x and y
return true; returns the boolean value true
return new apples; returns an uninitialized array of apple objects
When the return statement is reached in the method body, any code after will not be executed. Return literally returns to where the method was called, leaving the method and any code after is not reached.
Here is a live example of some method returning:
Methods must all have a unique name. While this is only partially true, it is important to know that a method's name really includes more than simply the method name itself.
A method signature is the method name plus the method's parameters. So two methods can share the same name, but specify different parameters. Here are some examples:
int addInts(int x, int y);
int addInts(int x, int y, int z);
int addInts(int x, int y, int z, int w);
Notice how all of these methods share the same return type and name, however, there parameters are different. Since both the name and the parameters make up the method signature, it is true that all methods must have a unique signature, otherwise Processing will not know which method to call.
Here is an example of overloading:
(This example will not work! Processing.js cannot overload methods, but try this example in regular Processing)
Since methods are separated blocks of code. When you declare a variable inside a method, the scope of that variable is the method body. This means that the variable does not exist outside the method. Let's look at an example:
Let's look at an example:
Variable scope is an important topic that we will discuss in more detail in relation to objects.