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) וגם 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.
לחץ על הרץ כדי לראות את התוצא כאן.