Goals

  • Understanding Methods
    • Creating Custom Methods
    • Adding Parameters
    • Returning
    • Overloading Methods
    • Variable Scope

Resources

http://processing.org/learning/basics/functions.html

"Art and science have their meeting point in method." ~ Edward G. Bulwer-Lytton

Creating Methods

In addition to setup and draw Processing allows us to define our own methods. This provides two important features:

  • We can separate our code
  • We can execute our code multiple times (with different arguments)

Methods are composed of a few parts:

  • Return Type: specifies what type of value will be returned (void for nothing)
  • Method Name: must be unique (there are exceptions)
  • Parameters: a list of variables with their type used in the method
  • Method Body: the code that the method executes

Calling Methods

We tell a method to execute its 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?

Here is an example of the drawStar method in action:

All methods must have a unique name.

Adding Parameters

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 to parameters:

  • A type
    • Each parameter must specify its own type
    • There are no shortcuts
  • A name
    • Each name should be unique
    • Do not use the same names as global variables

Arguments

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 a method.
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:

All parameters must have a unique name (no reserved words or global variables).
All parameters must specify a type.
When calling a method with parameters, the arguments must match the parameters.

Returning from a Method

Every method will return to where the method was called once it has finished executing its 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[5]; 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 not reached. Here is a live example of some method returning:

A method specifying a return type other than void, must return a value of that type using the return keyword.
After a return statement has been executed, no other statements in the method body will be executed. The method has returned to where it was called.

Method Overloading

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, their 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:
(Processing.js cannot overload methods, but try this example in regular Processing)

A method signature is the method name plus the parameters.
Each method must have a unique method signature.

Variable Scope

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:

Variable scope is an important topic that we will discuss in more detail in relation to objects.

A variable declared inside a method will only have scope inside that method.
To declare a variable that can be used everywhere in your sketch. Declare it outside the main setup and draw methods. This is referred to as a global variable in Processing.