Students turn ten lines that draw a flower into a function with parameters, call it with different values, find a call whose capitals do not match, draw a garden with a loop, write a function that returns true or false, and make a drawing function of their own.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Predict and Run 10 min · Investigate 15 min · Modify 15 min · Make 15 min
Read the unit primer first. This is the Python functions, parameters and returns units in one lesson, in JavaScript, so go gently on the third idea: the Modify task on isNear is the first time students write return. Predict is the key idea (a definition draws nothing). Names must match exactly; the playground hint for "is not defined" mentions capitals. Parameters take values in order, so a call with the arguments swapped moves the flower, not the size.
Names must match exactly, and parameters fill in order. Read the call and the definition aloud side by side.
| What you will see | A question to ask | The fix |
|---|---|---|
| A red line: drawflower is not defined. | Does the call spell the name exactly like the definition? | Match the capitals. |
| The flower is in the wrong place or the wrong size. | Are the values in the same order as the parameters? | Put the arguments in the order x, y, size. |
| isNear seems to do nothing. | Does the function use return, and is it used inside an if? | Add return before the comparison, and call it in the if brackets. |
| Nothing is drawn. | Is the function called anywhere? | Add a call in setup() or draw(). |
One possible solution for each task. Other correct programs are fine.
function drawFlower(x, y, size) {
fill(255, 120, 160);
circle(x - size, y, size);
circle(x + size, y, size);
circle(x, y - size, size);
circle(x, y + size, size);
fill(255, 210, 0);
circle(x, y, size);
}
function setup() {
createCanvas(300, 200);
background(120, 200, 140);
for (let i = 0; i < 5; i++) {
drawFlower(40 + i * 55, 60 + (i % 2) * 70, 14);
}
}
function drawFlower(x, y, size) {
fill(255, 120, 160);
circle(x - size, y, size);
circle(x + size, y, size);
circle(x, y - size, size);
circle(x, y + size, size);
fill(255, 210, 0);
circle(x, y, size);
}
function isNear(x1, y1, x2, y2) {
return dist(x1, y1, x2, y2) < 30;
}
function setup() {
createCanvas(300, 200);
}
function draw() {
background(120, 200, 140);
drawFlower(150, 100, 16);
if (isNear(mouseX, mouseY, 150, 100)) {
fill(255);
text("Hello!", 120, 30);
}
}
function drawTree(x, y, height) {
fill(120, 80, 50);
rect(x - 5, y - height, 10, height);
fill(40, 130, 60);
circle(x, y - height, height);
}
function setup() {
createCanvas(300, 200);
background(150, 210, 255);
fill(90, 170, 90);
rect(0, 150, 300, 50);
drawTree(60, 160, 60);
drawTree(150, 150, 40);
drawTree(240, 170, 80);
}
Stretch: Give the flower a colour parameter, or make drawFlower return the width it used.
Support: Give the definition of the drawing function and ask only for the three calls.
What is the difference between defining a function and calling it?
Defining writes the recipe and draws nothing; calling uses it with values and does the work.
Next lesson: the final challenge puts lists, objects, decisions and functions together in a click-to-catch game.