page13
Defining Functions
As well as using pre-prepared functions, you can also define your own. You must define a “setup()” and “loop()” function.
A weakness of processing is that the function definitions are a little complicated when you first meet them. This is due to the decision to build Processing on top of “C”. If you don't understand the detail, just treat this as a piece of magic that you can copy from examples as you need to.
Here is an example function definition from the “Blink” program.
void setup() {
pinMode(13, OUTPUT);
}
Name of the function: choose a name without spaces which is meaningful to you and doesn't overlap with any other function names. The names “setup” and “loop” have special meanings
Return types: Functions can return a value once they are completed. Simple functions don't do this, and to tell the computer you don't want to return a value you must put the word “void” in front of the function definition.
Function parameters: We have seen how you can use parameters when you call functions. When a function is defined the parameter definitions go here. If there are no parameters you must include a pair of empty brackets to show that the parameter list is empty.
Curly brackets around the function body: A pair of curly brackets goes round the “body” of the function. The body contains the instructions for what to do when the function is called.
Statements: The function body is made up of one or more statements that do the work of the function. Another function call is an example of a statement. Each statement is separated by a semi-colon.