5.1 Introduction to Methods
As your programs begin to get larger it becomes more difficult to manage them. It is helpful if you can break a large program down into smaller more manageable pieces. This is often called modular programming. One of the ways that Processing achieves modular programming is through the use of methods. In other programming languages these may be called procedures, functions or subroutines.
Modular programming has a number of advantages:
- allows us to focus on a small (easier) part of a larger problem
- easier to divide up the work among team members
- easier to reuse code (elsewhere in the same program or in other programs)
- easier to test small sections than the whole program at once. When we put the pieces together there is less likely to be errors
We have already seen and used some predefined methods. We've used methods to perform mathematical operations such as rounding real numbers and finding square roots. We've used many methods to do graphics. We've even written a couple of methods - draw and setup. In this chapter we will learn how to write and use our own methods from scratch.
The following method could be used to print an introduction to any of our programs. The first line looks like the setup method except this one is called printTitle. The other difference is that this method will not get called automatically at the start of the program. We have defined the method. We need to call this method from some other method (such as setup) to get the code to actually exectute. We call a method by using its name followed by a set of brackets. In this case we would call the method by saying: printTitle();
void printTitle()
{
println("-------------------------");
println("Another great program by:");
println();
println(" Me");
println();
println(" Created on January 10");
println("-------------------------");
}
|
|
Here is an example of a complete program using this method:
void setup()
{
printTitle();
println("This will get printed after the title.");
println("Let's print the title again");
printTitle();
println("Let do it one more time");
printTitle();
println("The end");
}
void printTitle()
{
println("-------------------------");
println("Another great program by:");
println();
println(" Me");
println();
println(" Created on January 10");
println("-------------------------");
}
|
|
The output from this program would be:
-------------------------
Another great program by:
Me
Created on January 10
-------------------------
This will get printed after the title.
Let's print the title again
-------------------------
Another great program by:
Me
Created on January 10
-------------------------
Let do it one more time
-------------------------
Another great program by:
Me
Created on January 10
-------------------------
The end
|
The order that you put the methods in your program doesn't matter. We could have done the printTitle method first and then setup. Our program is much shorter than it would have been if we had had to copy and paste the lines inside printTitle three times.
Normally a method runs until you get to the closing brace bracket. If you want to exit before then you can use the return statement. Suppose you wanted to generate some random division questions and give the answers. You would want to make sure you don't divide by 0. Here is an example of such a method:
void divisionQuestion()
{
int x, y;
x = (int)random(-10, 11);
y = (int)random(-10, 11);
if (y == 0)
{
println("Sorry we chose 0 for the denominator");
return;
}
else
println(x + " divided by " + y + " is " + x / y);
}
|
|
Exercise 5.1
- Explain the difference between a method definition and a method invocation (call).
- In the following program, the executable statements are numbered. Use these numbers to indicate the order in which the statements are executed.
void printChorus ()
{
println(); //1
println("Ee-igh, ee-igh, oh!"); //2
println(); //3
}
void setup()
{
println("Old MacDonald had a farm"); //4
printChorus(); //5
println("And on that farm he had" + " a pig"); //6
printChorus(); //7
}
- Rewrite the divisionQuestion method so that it avoids the use of a return statement.
- Write a method that will simulate the results of rolling two fair dice by printing two random integer values in the range 1 to 6 along with their total. Sample output from a call to the method could be:
| |