We’ve seen functions already in the context of organizing our programs, and they’re great for that. The next step is seeing that we can provide information to a function so that it can do its job even better.
Let’s check out the Trisolaris project here.
Not bad. We’ve broken the task down into functions, so it’s modular: it’s broken down into meaningful parts.
But right now there’s something a bit awkward going on. Each of our functions for drawing a sun is almost identical. That’s a sign that we need to think about the other great thing that functions can give us: reuse.
It would be better if we could write one drawSun() function that could take care of drawing each of our different suns, which only differ in terms of their x, y, and size. And we can, using parameters.
We already see this trick in, for example, the ellipse() function, where we’re able to specify the x, y, and diameter of a circle. That allows us to reuse the ellipse() function all over the place! It’s very versatile! It’s more general!
So let’s define and use our own function with parameters that can draw a Trisolarian sun in any position and at any size!
Here’s our drawSun() definition improved with parameters:
function drawSun(x, y, size) {
push();
noStroke();
fill("#f99736");
ellipse(x, y, size);
pop();
}
You can see it’s very similar to the original idea, except:
We call our new function by providing the arguments (values) that should go into each parameter in order. So to draw the first sun we’d write:
drawSun(500, 100, 80);
400 will go into the x parameter of the function (and eventually will be used for the x-coordinate of the circle)100 will go into the y parameter of the function (and eventually will be used for the y-coordinate of the circle)80 will go into the size parameter of the function (and eventually will be used for the size/diameter of the circle)That’s the whole trick! To write and use a function with parameters you:
Now that we have a function with parameters we can use it more than once! What if we’re on Trisolaris (from the novel The Three-Body Problem by Liu Cixin), where there are three suns? No problem!
// Draw the three suns of Trisolaris
drawSun(500, 100, 80);
drawSun(350, 180, 200);
drawSun(120, 100, 160);
Each time we call our drawSun() function we’re providing different values that go into the corresponding x, y, and size parameters and so end up drawing a different sun.
But it’s still the same function each time.
And that’s reuse!
Consider:
drawSun() once and affect all three suns!By using paramters in our functions we make them vastly more flexible and reusable in our program. Using parameters is often the key to writing a function that will reduce repetition your program.
/**
* Trisolaris
* Pippin
*
* Draws the three suns of Trisolaris. Poorly.
*/
"use strict";
/**
* Create the canvas
*/
function setup() {
createCanvas(600, 400);
}
/**
* Draw the three suns
*/
function draw() {
// Sky blue
background("#87ceeb");
// Draw the three suns of Trisolaris
drawSun(500, 100, 80);
drawSun(350, 180, 200);
drawSun(120, 100, 160);
}
/**
* Draws a sun at specific position and size
*/
function drawSun(x, y, size) {
push();
noStroke();
fill("#f99736");
ellipse(x, y, size);
pop();
}