Lesson 38: Parameters
此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
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 |
English
The turn at each corner
| Shape | Sides | Turn |
|---|---|---|
| triangle | 3 | 360 ÷ 3 = 120° |
| square | 4 | 360 ÷ 4 = 90° |
| hexagon | 6 | 360 ÷ 6 = 60° |
English
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.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
The starter has one function for each shape. Replace them with 一个 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) and polygon(6, 200).
点击“运行”查看此处输出。
难度 4/5 · 今日首次完成可获 280 金币及少量额外奖励
The loop draws shapes with 3 to 8 sides, each with 150 mm sides. Write a 功能 perimeter(sides, length) that 返回值 the length of the outline (sides × length). Add each result to ink, so the program prints Ink: 4950 mm at the end.
点击“运行”查看此处输出。