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
LO: To use a variable(s) in a function.
We got a bit serious with the whole 'you must include a return value' last time and so far we haven't used one, but we will, we will!
Now we are going to look at writing functions that accept more than one input (arguments).
The best thing to use for writing these kind of functions are written mathematics problems as the children are used to identifying the key numbers (to be variables). We will use comments to include the question so as to give some context to the program.
#Terry's parcel weighs half a kilogram. #Graham's weighs 300g. #How much do they weigh together? #declare variables for the key information terry = 500 graham = 300 #define a function requiring two arguments def add_weights(weight1, weight2): #seperate the arguments with a comma print(weight1 + weight2) return (weight1 + weight2) #remember to return a value, don't forget that space! #call the function inputting the two variables as arguments add_weights(terry, graham)
Let's have another look at a harder mathematical written question...
#Train tickets for two adults and two children cost £24 altogether. #Children's tickets cost half price. #How much is the adult ticket? #declare variables for the important numbers total_price = 24 #here we need a function to take in one argument def train_tickets(total): #we will call the argument total .....????????? #call the function including the argument train_tickets(total_price)
This a great example to show that using programming will not save us from having to think! lets use the bar method to help us here:
By looking at the bar method we can see that this problem will be formed of two parts - one to divide by 3 and then to divide that answer by 2. We will need to use a variable to 'remember' the intermediate answer, let's have a look at what that might look like:
#Train tickets for two adults and two children cost £24 altogether. #Children's tickets cost half price. #How much is the adult ticket? #declare variables for the important numbers total_price = 24 #here we need a function to take in one argument def train_tickets(total): #we will call the argument total divide = (total / 3) #declare a variable to hold to intermediate number answer = (divide / 2) #declare a variable to hold the final answer print(answer) #print out the result return answer #return the value, remember the space! #call the function including the argument train_tickets(total_price)
Just to make things clear, we have declared a variable to remember the total price total_price. The function train_tickets(total) requires one argument named total. When we call the function train_tickets(total) we are inputting the value of total_price as the argument, we are saying that total_price = total.
Finally, let's look at a problem that will make use of the return function - THIS IS ONLY AN EXTENSION EXERSICE TO DEMONSTARTE THE USE OF THE RETURN FUNCTION!!! where purchasing tickets will incure a booking fee, a fee that will be added to any amount of tickets purchased:
#Train tickets cost £5. #Three tickets are purchased. #Booking fee is £2 #declare variables tickets = 5 booking_fee = 2 #let's write a function that adds the booking fee def booking(price): #the function takes an argument called price total = price + booking_fee #calculate the booking fee return total #return the total plus the booking fee #define a function that calculates the total price of tickets def total_cost(number_of_tickets): total = number_of_tickets * tickets #multiply the number of tickets by the cost #send the cost of the tickets to the booking fee function by declaring a variable to remember the value and calling the function including the total final_price = booking(total) #the booking function will return the value of the total plus the booking fee print(final_price) #print out the result return final_price #return the value (it might be used later on) #call the function including the argument for 6 tickets total_cost(6)
Let's look carefully at the booking function:
If we look again at the picture representing a function, the booking function accepts one input, price as an argument and then returns the value total after it has added the £2 booking fee. We first declare a variable to remember a value and then call the function including the total cost of the tickets purchased final_price = booking(total): the variable final_price will hold the value of the booking function's return value (after it has added the £2 booking fee). A visual representation of this would look like:
The function total_cost is called with the argument 6 to represent the number of tickets required.
The function total_cost takes the argument 6 and multiplies it by the cost of a single ticket tickets
The total variable holds the value of the cost of all the tickets. The variable cost is then sent to the function booking as an argument.
The function booking takes the argument and then adds the booking fee before rturning the value to the total_cost function.
The total_cost function then takes that value and prints out the final_price as well as returning the value (of course).