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

4.5a

LO: To use a variable(s) in a function.

Now is a great time to look very carefully at our picture of using functions:

The image of a function as a box is a very important one: a function excepts an input (parameters, made up of an argument or arguments) and then returns a value. If we look at the picture, then we don't know what is inside the box - it is actually important that we don't need to know; just like when we use the print function, we know that we can enter a string or a number and it will print these out to the screen without us having to worry about how this is done. This is a very important principal of programming! A function does something to an input and then returns a value - how it does it is not important to us. This is very important when children are turning mathematical problems into programs, for example:

#Find the perimeter of a square.
#declare a variable for the length of one side
side = 7
#define a function to work out the perimeter of the square taking in the argument of the length of one side
def perimeter1(side):
    answer = side * 4
    print(answer)
    return answer

define another function to work out the perimeter of the square
def perimeter2(side):
    answer = (side + side + side +side)
    print(answer)
    return answer

 call the functions with the correct parameter
 perimeter1(side)
 perimeter2(side)

			

As you can see, both functions are correct, they both print out the correct answer. Is one function more correct than the other?
Here we can see the benifit of looking at functions as boxes - if I sent either function the argument 7 I would get the same answer so as a user I don't care what happens in the function, I just care about the correct answer. Here we can see that computing has endless oportunities for extending children - the author of perimeter2 could be asked if they could think of a way of improving their code, but at the end of the day both functions answer the problem. A more imprtant factor is readability, consider this example:


s = 13

def x(s):
    print(s + s)
    return (s + s)

x(s)
			

This is the same problem we looked at in 4.4a but by using unhelpful variable names it is harder to follow what is happening in the program - this is a greater problem than using different methods of calculation, by looking at the code we can't work out what the program is trying to achieve although it is correct.



Let's look at one more problem of working out the perimeter of a four sided shape:

#working out the perimeter of an irregular four sided shape

#define a function to add the sides
def four_sided_shape(side1, side2, side3, side4): #argument for all sides
    perimeter = (side1 + side2 + side3 + side4)
    print(perimeter)
    return perimeter

#call the function including the four sides
four_sided_shape(4,5,9,7) #arguments are seperated by commas
			

Have fun turning mathemaical problems into programs!

Code Examples

Resources