AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Ready-made helpers

Week 10 · 1.6 Library programs · 3 lessons
2

Sending values in

Computing 8: Topic 1.6, pp. 125–149
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.

20+ 424
Worked examples
Python
1def add_delivery(price):
2    return price + 4
3
4print(add_delivery(20))
Output
24
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):
2    print("Your order is ready,", name)
3
4show_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.
Python
1def pay(price, coupon):
2    return price - coupon
3
4print(pay(20, 3))
5print(pay(3, 20))
Output
17
-17
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):
2    return n * 3
3
4x = triple(4) + 1
5print(x)
2
Write a function with_tip(bill) that returns the bill plus 2, and a line that prints with_tip(18). What is printed?
3
Is each sub-routine a procedure or a function? Give a reason.
Python
1# A
2def show_menu():
3    print("Soup or salad")
4
5# B
6def double(n):
7    return n * 2
8
9# C
10def ticket_price(age):
11    if age < 12:
12        return 5
13    else:
14        return 9
4
A pupil expects total to be 12, but the program prints 12 and then None. Explain what went wrong and fix it.
Python
1def add_tax(price):
2    print(price + 2)
3
4total = add_tax(10)
5print(total)