Functions
Functions are an important structural element in computer programming. They are a key way of breaking your code up into small, general, reusable parts. Breaking up programs into small parts is essential as programs get more complex, because it allows you to focus on one problem at a time helping you manage complexity. Creating general, reusable parts is an important part of reducing repetition in your code. Repetition makes programs needlessly complex, and harder to maintain. Programmers sometimes speak of keeping your code DRY.
Functions are basically small subprograms that you can use together as “blocks” in creating your larger program. You have already been using functions quite a bit. When you use “rect()”, “line()”, or “fill()”, you are calling a function. You have also declared and defined “setup()” and “draw()” functions. All that we are introducing today is creating a function you name and call yourself.
Functions can be broken down into a few parts:
-
return type – Sometimes functions send a value back to the code that calls them. When you declare the function, you specify the type of this value (int, float, etc.) or specify “void” for functions that do not return a value.
-
name – Like variables, functions have names. Also, like with variables, you should choose a name that clearly indicates what the function does. Variable names are almost always nouns. Function names should almost always start with a verb.
-
parameters – Functions often receive parameter values from the code that calls them. This lets the calling code specify additional information that the function needs. For example, a function that draws a star on the screen might take parameters for the number of points in the star.
-
body – In between the braces, “{” and “}”, you place the code that will run when the function is called.
-
return statement – The return statement is used to send a value back to the code that called the function. When a return statement is executed, the function stops running immediately. If there is more code below the return statement, it will be ignored.
Example Functions:
//Calling this function will set the fill to a random color.
void randomFill()
{
fill(random(0, 255), random(0, 255), random(0, 255));
}
//This function draws a red and white ring target. Passing values for _x and _y lets you tell the function where to draw.
void drawTarget(float _x, float _y)
{
noStroke();
fill(255, 0, 0);
ellipse(_x, _y, 50, 50);
fill(255, 255, 255);
ellipse(_x, _y, 40, 40);
fill(255, 0, 0);
ellipse(_x, _y, 30, 30);
fill(255, 255, 255);
ellipse(_x, _y, 20, 20);
fill(255, 0, 0);
ellipse(_x, _y, 10, 10);
}
//This is a silly function that adds to parameters and returns the results. You wouldn't want to use this function as the + operator already does this. I'm including it to show the structure of a function.
float add(float _a, float _b){
return _a + _b;
}
Group Discussion of Proposals
One on One Meetings
Homework
-
Read Chapter 8 in the Getting Started book. Also, if you haven’t already, read chapters 6 and 7.
-
Make sure you understand functions. Take extra time to with chapter 8, create some functions on your own, review examples that use functions. Make sure you are comfortable with how to create and call functions.
-
Your prototype is due next week. Demonstrate that you understand functions and can use them by including at least one function in your prototype. Be ready to show and discuss your prototype in class.