GoalUse parameters to send values into a sub-routine, and tell a function from a procedure.
New words
parameter · a variable in the brackets of a sub-routine that receives a value when it is calledprocedure · a sub-routine that does a job but does not return a valuefunction · a sub-routine that returns a value
Explanation
Many sub-routines need information to do their job. A parameter is a variable in the brackets after the name. When the sub-routine is called, the value in the brackets of the call is stored in the parameter. So add_delivery(20) stores 20 in price. A function returns a value, so the call can be used in a sum or printed. A procedure carries out a job, such as showing a message, but does not return a value. Both are sub-routines. A sub-routine can have more than one parameter, separated by commas. The values are matched in order: the first value goes to the first parameter. Giving the values in the wrong order can give a wrong answer.
A function with one parameter. The call add_delivery(20) stores 20 in price. The function returns 20 + 4, which is 24. The function machine in the explanation shows the same idea: a value goes in, a rule is applied, and a new value comes out.
Python
1def show_banner(name):
2print("Your order is ready,", name)
34show_banner("Ana")
Output
Your order is ready, Ana
A procedure: it shows a message but does not return anything. The call is a whole line on its own. A function call, by contrast, is used as part of a line, for example inside print or a sum.
Two parameters. The values are matched in the order they are written. pay(20, 3) puts 20 in price and 3 in coupon. pay(3, 20) swaps them and gives a very different answer.
Class activity
Function machine (no computer). Draw a machine with an arrow in and an arrow out. Write the rule inside, such as add 4 or multiply by 3. A partner calls a function with a value, such as with_tip(9). You put the value through the machine and say the returned value. Make a second machine with two inputs, for example pay(price, coupon) with the rule price − coupon. Test pay(20, 3) and pay(3, 20) and discuss the difference. Then write a short procedure and decide how it is different from a function. If you have a computer or tablet, type one function and one procedure.
Practice
1
Trace the call. What is printed?
Python
1def triple(n):
2return n * 334x = triple(4) + 15print(x)
triple(4) returns 4 * 3, which is 12. Then x = 12 + 1 = 13, so the program prints 13.
2
Write a function with_tip(bill) that returns the bill plus 2, and a line that prints with_tip(18). What is printed?
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): def with_tip(bill): / [indent] return bill + 2 / print(with_tip(18)). Note: It prints 20.
3
Is each sub-routine a procedure or a function? Give a reason.
Python
1# A2def show_menu():
3print("Soup or salad")
45# B6def double(n):
7return n * 289# C10def ticket_price(age):
11if age < 12:
12return513else:
14return9
A is a procedure: it prints a message and returns nothing. B is a function: it returns a value. C is a function: it returns 5 or 9 depending on the age, so the call can be used in a sum.
4
A pupil expects total to be 12, but the program prints 12 and then None. Explain what went wrong and fix it.
add_tax uses print instead of return, so it is a procedure: it shows 12, but gives nothing back. The variable total then holds None, which means no value. Fix: change print(price + 2) to return price + 2, and then print(total) shows 12 once.