CP- Functions Lesson
Function Lesson
We know that a function is a procedure for performing a specific task. We also know that though Processing calls these functions, they are also referred to as "procedures" or "methods" in other programming languages. So far we have created static sketches. This means that the program doesn't do anything after the code has executed.
Processing provides two predefined functions that are used to create active sketches. Almost every Processing program you write will have these two functions.
Let's look at the functions in detail. The text of a program is called source code or code for short.
The source code is divided into two functions, setup() and draw().
The setup() Function:
The setup( ) block is run once to set up the initial information about the drawing. Notice that it begins with public void. All functions must begin with public and the return type.
The information inside the setup() function is usually the
- Size of the frame, and
- Set initial value of variables and colors and
- Set the background optional according to the sketch
The draw() Function:
The draw() function is called continuously while the program runs. It is similar to a game loop. It will be repeated over and over.
The draw function() draws everything inside the body of the function at 60 frames per second. The program above will continually draw an ellipse that is 50 pixels by 50 pixels at the mouseX and mouseY location.
public void draw( )
{
<drawing instructions go here>
}
Both setup() and draw() are void functions. They do not return a value. They perform an action. We will look at functions that return a value later.
Creating Function Signatures
The following shows the layout for creating a function or method. The signature consists of the function or method name and parameter list.
Explanation of the parts of the function header:
- Visibility: for now just know that this is going to be the keyword public. The access specifier public means that his method can be called in any class.
- Return type: some methods do not just accept values through the parameter list, but they give you back values. These are return values. The return type tells us what kind of data is being returned. When we do not have a value coming back from the method, the return type is void.
- The method or function name: is a very important part of the method. It should follow the rules for identifiers and should start with a lowercase letter by convention.
- Parameter list: this is like declaring a variable to use in the method. This is information the function needs in order to execute and are enclosed in parentheses after the method name. You must always supply a parameter even if it is empty.
- Method or function body: enclosed in braces. The entire function/method begins and ends with a brace. { }
Local Versus Global Variables and Scope
Now that we are creating functions using setup and draw, we need to discuss the difference between a global variable and a local variable. The reason this is important has to do with the term scope. Scope refers to where a variable can be seen inside the program.
Global Variables are declared at the top of the program outside of any function that we create. These variables can be initialized in setup() and then used and updated anywhere in the program.
Local Variables are declared inside a function or block of code: setup(), draw(), or other functions we create. A local variable declared inside a function can only be seen and used inside that specific function where it was declared. If you try to access a local variable outside of the function where it was declared you will get an error message.
[CC BY 4.0] UNLESS OTHERWISE NOTED | IMAGES: LICENSED AND USED ACCORDING TO TERMS OF SUBSCRIPTION