Give a piece of code a name, hand it values with parameters, and get an answer back with return.
One flower took ten lines. A function turns those lines into a single word, so a whole garden is three short lines.
A function is code with a name. You write it once, then call it as often as you like: drawFlower(80, 100);.
The names in the brackets of the definition are parameters: function drawFlower(x, y) says "give me an x and a y". Each call fills them in, so one function draws flowers in many places.
A function can also hand an answer back with return, and you will write one in the Modify step.
The hard part is the name. drawFlower and drawflower are different names, and JavaScript will not guess. If you see "is not defined", compare the call with the definition letter by letter.
Make your prediction first. Help opens once you've locked it in.
function drawFlower(x, y) {
fill(255, 120, 160);
circle(x - 14, y, 18);
circle(x + 14, y, 18);
circle(x, y - 14, 18);
circle(x, y + 14, 18);
fill(255, 210, 0);
circle(x, y, 18);
}
function setup() {
createCanvas(300, 200);
background(120, 200, 140);
drawFlower(80, 100);
drawFlower(200, 60);
}
Does the definition draw anything, or only the calls?
The function now has a third parameter, size. Each call chooses where the flower goes and how big it is.
A function can hand an answer back with return. Here twice(4) is the number 8, so it can be used anywhere a number goes:
function twice(n) {
return n * 2;
}
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);
drawFlower(70, 100, 20);
drawFlower(180, 60, 12);
drawFlower(230, 140, 16);
}
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);
drawFlower(70, 100, 20);
drawFlower(180, 60, 12);
drawFlower(230, 140, 16);
}
Definition, call, parameter, return.
Where each number goes.
One letter of difference.
Match each piece with what it is.
This stops with an error: drawflower is not defined. Click the line that causes it.
Use a loop to draw five flowers with one call.
Replace the three calls with a for loop that counts to 5.
Use i in the call: drawFlower(40 + i * 55, 100, 14).

Make the flower say hello when the mouse is near it, using a function of your own that returns true or false.
A function that answers true or false is used inside an if.
Write function isNear(x1, y1, x2, y2) { return dist(x1, y1, x2, y2) < 30; } and use if (isNear(mouseX, mouseY, 150, 100)).
Draw five flowers with one call inside a loop.
Run your program. This ticks when your code does all of the above.
Write a function that returns whether the mouse is near the flower, and say hello when it is.
Run your program. This ticks when your code does all of the above.
Write a function that draws something of your own (a tree, a house, a robot, a face) with at least a position as parameters. Then call it three times. The sketch on this page draws trees of different heights.
You can now name your own code. Next: put lists, objects, decisions and functions together in a game.
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. About 55 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
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.
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.
Next lesson: the final challenge puts lists, objects, decisions and functions together in a click-to-catch game.