Week 24 · Unit 10: Be a game developer · 3 lessons
3
Variables in a formula
Computing 5: Unit 10, pp. 129–144
GoalUse variables in a formula, such as the distance round a rectangle, and tell variables from constants.
New words
formula · a rule that shows how to work out an answer from other valuesperimeter · the distance all the way round the edge of a shapevariable · a named place that stores a value that can changeconstant · a value that never changes
Explanation
A formula tells the computer how to work something out. The perimeter of a rectangle is the distance round its edge, so P = l + w + l + w. The letters l and w stand for the length and the width. They are variables, because every rectangle can have different sides. The program asks for the values, stores them, and works out P. The next time it runs, new values give a new P, but the formula stays the same. A constant is a value that never changes. A tricycle always has 3 wheels, so the wheels of t tricycles are W = 3 × t, where t is a variable and 3 is a constant.
l
w
P = l + w + l + w
8
5
26
6
2
16
10
10
40
Worked examples
Step
Instruction
Values
1
Ask for the length and store it in l
l = 8
2
Ask for the width and store it in w
w = 5
3
Set P to l + w + l + w
P = 26
4
Output P
shows 26
A gardener fences a rectangular vegetable bed. This algorithm works out the fence that is needed. The bed is 8 m long and 5 m wide, so P = 8 + 5 + 8 + 5 = 26 m.
In Scratch the formula uses nested blocks. If the user types 6 and 2, the sprite says 16.
Tricycles t
3 × t
Which values changed?
4
12
t and the answer
7
21
t and the answer
the 3
never changes
a constant
Look at a tricycle shop. The number of wheels on one tricycle is a constant. The number of tricycles in the shop is a variable.
Class activity
Rope rectangles (pairs, a piece of string and a ruler, or a shoe-lace). Lay the string to make a rectangle on the table. Measure the length and the width in hand-spans, write them down, and calculate P = l + w + l + w. Then check by measuring the string. Make three different rectangles with the same string. What do you notice about the perimeters? Which values changed and which stayed the same?
Practice
1
Which of these are variables and which are constants? (a) the wheels on a bicycle, (b) the lives left in a game, (c) the sides of a hexagon, (d) the age of a growing plant.
Thing
a
the wheels on a bicycle
b
the lives left in a game
c
the sides of a hexagon
d
the age of a growing plant
(b) and (d) are variables because their values change. (a) 2 and (c) 6 are constants.
2
The user types 9. What does the sprite say? Which value in the program is a constant?
It says ‘In 5 years: 14’, because 9 + 5 = 14. The 5 is a constant. age and future are variables, because they change when someone else types an age.
3
Write an algorithm that asks for the morning temperature and the afternoon temperature and outputs how many degrees warmer the afternoon was.
1 Ask for the morning temperature and store it in Morning. 2 Ask for the afternoon temperature and store it in Afternoon. 3 Set Rise to Afternoon − Morning. 4 Output Rise.
4
An egg box always holds 6 eggs. Use E = 6 × b to work out the eggs in 5 boxes and in 9 boxes. Which part of the formula is a constant?
5 boxes give 30 eggs and 9 boxes give 54 eggs. The 6 is the constant, because one box always holds 6 eggs. The number of boxes b is a variable.