Lesson 38: Parameters
One procedure, many shapes
- A procedure without parameters always does the same thing.
- A parameter is a value you give to a procedure when you call it. The same code then draws a triangle, a square or a hexagon.
- A function does a job and returns a value to the code that called it.
Words for this lesson
| Word | Meaning |
|---|---|
| procedure | a named block of code that does a job |
| function | like a procedure, but it returns a value |
| parameter | a value passed in when the procedure is called |
| call | run a procedure or function by its name |
The turn at each corner
| Shape | Sides | Turn |
|---|---|---|
| triangle | 3 | 360 ÷ 3 = 120° |
| square | 4 | 360 ÷ 4 = 90° |
| hexagon | 6 | 360 ÷ 6 = 60° |
In the exam
- 0478/22/O/N/23 Q8: why use procedures and parameters? Mark scheme: procedures put many statements under one identifier, can be re-used, and make programs shorter and easier to maintain; parameters pass values from the main program to a procedure, so it can be re-used with different data.
- 0478/21/M/J/24 Q2(c): the role of procedures and functions in a program that is easier to maintain. Mark scheme: they divide the program into smaller parts that are easier to read and debug, can be re-used, and reduce repeated code.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
The starter has one function for each shape. Replace them with một procedure polygon(sides, length) that can draw any regular shape: sides times, drive length mm and turn right 360 / sides degrees. Then draw three shapes from the same corner: polygon(3, 400), polygon(4, 300) và polygon(6, 200).
Nhấn Chạy để xem kết quả ở đây.
Độ khó 4/5 · 280 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
The loop draws shapes with 3 to 8 sides, each with 150 mm sides. Write a hàm perimeter(sides, length) that returns the length of the outline (sides × length). Add each result to ink, so the program prints Ink: 4950 mm at the end.
Nhấn Chạy để xem kết quả ở đây.