Processing has several functions like ellipse(), rect(), and fill() Those functions are provided by the makers of Processing to make our life easier when drawing and coloring shapes.
We can also make our own functions! Functions are great for making reusable code. If we perform the same task over and over again, we can simply call the function rather than rewriting the same block of code over and over again.
Why use functions?
We also use functions to break up our code into small, easily read parts. So far, we have only made small programs with one big chunk of code, but when you create a program with 200 or more lines, breaking your code up into functions will help you not only read your code but also fix errors in your code
Syntax
Sometimes we use functions to calculate a value and then return that value to the main program. It is important to note that a function can only return ONE value. However, we can pass as many parameters as we want to the function.
The
function name
is the name we call whenever we want to use a function in our program
The
return type
is the data type that the function will return A function that returns a value must use the return keyword in the program. The return
keyword tells Processing to return a particular value from the program.
Parameters
are the values inside the parenthesis. A function can have as many parameters as you want. Each parameter must have a data type associated with it.

Using a Function:
In order to use a function, we must call it from our main body of code. To call a function we simply write the function’s name followed by the parameters expected by the function. For example, we would call the above program like so:
multiplyTwoNumbers(4, 5);
Since the function is expecting two integers as parameters, you MUST call the function with two integers. An error will occur if you pass the wrong data type or if you pass too many or too little parameters.
Function Example
int multiplyByFive(int n) { return n * 5; } void setup() { println(“Multiply 3 by 5: “ + multiplyByFive(3)); }
Sometimes we do not want to return anything from a function. When that happens the return type is called
void
. When the return type is void we do not use the return keyword.

The setup() and draw() Functions
Two functions,
setup() and draw() are special functions offered by Processing. These two functions are never called by us, rather Processing calls them automatically.
Leave a Reply