We give information to a method by passing arguments, but some methods also give us information back in return. This is called the return value of the method. For example, the main Processing object includes a method named random. When we call the random method, we provide two numbers as arguments. Processing’s random method then returns to us a random number between the value of the first argument and the value of the second argument. If we call, random with 10 and 100 as the arguments, like so: random(10,100), then we will get back a random number between 10 and 100.
We can use the number that random returns wherever in our program a number would be appropriate. Figure 1.1 shows the code to create a Monster with a random width by replacing the width argument in the first call to the ellipse method with random(10,100).
size(800,400); ellipse(100,125,random(10,100),50); line(100,125,100,135); ellipse(90,125,5,5); ellipse(110,125,5,5); rect(90,140,5,6); rect(90,140,5,6);
Each time you run the program, the width of the ellipse for the Monster’s head will be different. If random returns 50, then 50 is passed in as the argument for width in our call to ellipse. If random returns 90, then 90 is passed in as the argument for width in our call to ellipse. You can run the program by pressing the Start button in Figure 1.2. Pressing Restart will display the Monster with various widths based on the return value of random.