design, write and debug programs that accomplish specific goals, including controlling or simulating physical systems; solve problems by decomposing them into smaller parts.
use logical reasoning to explain how some simple algorithms work and to detect and correct errors in algorithms and programs
3.5b
LO: To write programs with more than one function.
Variables are also a great way of sharing data - the same variable can be used in different ways. For this example we'll use the idea of the area and perimeter of a square.
The area of a square is calculated by multiplying the length of a side by itself. The perimeter of a square is calculated by multiplying the length of a side by 4. In both of these calculation the length of a side is needed so this will be our shared variable.
#Year group or class
#name
#date
#Area and perimeter of a square
#declare a variable and assign it a value
side = 10
#define a function to calculate the area
def area():
print(side * side)
#define a function to calculate the perimeter
def perimeter():
print(side * 4)
#call the functions to perform the calculations
area()
perimeter()
We can see again the benifit of using a variable to stand for a number - if we needed to work out the calculations for a different size square then we would only have to change one number.
Children can experiment with using variables with different functions, unit conversions are great to do - for example m to cm and mm.