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.3a

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

We last looked at functions back in year three where we learnt that a a function was a way of teaching Reeborg a new skill. The good news is that functions in Python are the same so we can build on the skills we learnt then, with some added new stuff.

#a function from year3def name(): do_something()#call the functionname()

This was fine for teaching Reeborg some new tricks but now we can begin to understand functions a bit more.

Lets look at a simple function in mathematics:

f(x) = 2x

This function takes an argument x and returns 2 multiplied by x. In other words, if I put 3 into the function, I would get 6 out.

We have introduced two very important new words into our understanding of functions - *argument* and *return* - these can very roughly be thought of as our inputs and outputs. Think of a function machine...

You give the function an input (an argument), the function does something to the input and then outputs it (a return value). In fact, we have already come across a function that works like this: print()

The print() function takes in input as arguments inside the parentheses, for example print("Hello") takes in a string. The print() function then does something to print out that input to the screen (the output).

Let's look at a 'real' maths problem and see if we can write a function to solve it:

#John has 13 sweets.#Eric has twice as many.#How many sweets does Eric have?#declare variables for the important numberssweets = 13#here we need a function to take in one argumentdef sweet_problem(num_of_sweets):#we will call the argument num_of_sweetsprint(num_of_sweets * 2)#we print out the answerreturn (num_of_sweets * 2)#we return the value of the input multiplied by 2 (notice the space!!!!)#we call the functionsweet_problem(sweets)#the function expects one argument and we input the variable sweets

Let's look at this in some more detail:

- We declare a function that will accept one input - we give a name to that argument - num_of_sweets, we could have called this anything but num_of_sweets does explain what it is expecting.
- Inside the function we use the name of the argument in our calculation (num_of_sweets * 2) and we
*both*write a return statement*and*print the answer out. Here we are just insilling good practice, looking at the example program it is obvious that we do not need a return statement (it is not used) but now is a good time as any to start the habit of including one in every function we write from now until forever!

Ask the children to have a look at simple mathematics questions to turn into functions using *sensible*, *practical*, *helpful* names for variables and functions.