AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Scores and sums

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.

lwP = l + w + l + w
8526
6216
101040
Worked examples
StepInstructionValues
1Ask for the length and store it in ll = 8
2Ask for the width and store it in ww = 5
3Set P to l + w + l + wP = 26
4Output Pshows 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.
whenclickedaskLength in metres?and waitsetlengthtoansweraskWidth in metres?and waitsetwidthtoanswersetperimetertolength+width+length+widthsayperimeter
In Scratch the formula uses nested blocks. If the user types 6 and 2, the sprite says 16.
Tricycles t3 × tWhich values changed?
412t and the answer
721t and the answer
the 3never changesa 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
athe wheels on a bicycle
bthe lives left in a game
cthe sides of a hexagon
dthe age of a growing plant
2
The user types 9. What does the sprite say? Which value in the program is a constant?
whenclickedaskHow old are you?and waitsetagetoanswersetfuturetoage+5sayjoinIn 5 years: future
3
Write an algorithm that asks for the morning temperature and the afternoon temperature and outputs how many degrees warmer the afternoon was.
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?