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

Term 1 check-in

Week 11 · Review: Term 1 · 3 lessons
3

Search, call and return

Computing 8: Topics 1.1–1.6, pp. 9–136
GoalTrace a linear search, and trace functions that take a parameter and return a value.
New words
linear search · checking items one after another from the first, until the target is found or the end is reachedparameter · a value sent into a sub-routine, such as km in delivery_fee(km)return value · the value a function sends back to the place where it was calledlibrary · a collection of ready-made sub-routines that a program can use
Explanation

A linear search looks at the items one at a time, in order, from the first. It stops when it finds the target, or when the last item has been checked. If the target is not there, it makes one look at every item before it reports that it was not found. A function is a named block of code that hands a value back to its caller with the word return. When you trace a program you replace each function call by the value it returns. A procedure does a job, such as printing, but does not return a value, so it cannot be used inside a sum. A library gives you ready-made sub-routines with import. Libraries save time, but code from an untrusted place can hide mistakes or harm, so always ask a trusted adult.

1?2?3?4?5?
Worked examples
1✗2✗3✗4✓5
Five toolboxes in a row each hold one item: a hammer, a saw, a drill, tape and glue. A linear search for tape opens toolbox 1, then 2, then 3, and finds the tape in toolbox 4. That is 4 looks. A search for paint opens all 5 toolboxes and reports Not found.
Python
1def delivery_fee(km):
2    if km <= 5:
3        return 3
4    else:
5        return 6
6
7total = delivery_fee(4) + delivery_fee(9)
8print(total)
Output
9
A delivery fee function. To trace the program, replace each call by its returned value. delivery_fee(4): 4 <= 5 is true, so it returns 3. delivery_fee(9): 9 <= 5 is false, so it returns 6. Then total = 3 + 6 = 9.
Sub-routineParameterReturns a value?Can be used in a sum?
double(n): return n * 2nyesyes
welcome(name): print("Hi", name)namenono
Two sub-routines compared. The function can be part of a calculation, because it gives back a value. The procedure only prints, so nothing comes back to the place where it was called.
Class activity

Cup detectives (no computer). Use six paper cups in a row, numbered 1 to 6. A partner secretly puts a different small object in each one. You name a target. Lift the cups in order, say each position aloud, and count your looks until you find it. Then your partner chooses an object that is not there, and you count again. Next, play function machine: your partner says a rule such as add 3 and a number, and you say the number that comes out. Try one rule with two parameters, for example subtract the second from the first. If you have a computer or tablet, an adult can help you type delivery_fee and test it.

Practice
1
The list is compass, eraser, stapler, tape, marker, scissors. How many items does a linear search look at for (a) tape, (b) compass, (c) paper? What does it report in each case?
Position123456
Itemcompasseraserstaplertapemarkerscissors
2
What does this program print? Show the value returned by each call.
Python
1def tax(price):
2    return price + 3
3
4x = tax(10)
5y = tax(x)
6print(x, y)
3
This program stops with an error. Say which error message Python shows in the last line, why it happens, and how to fix the function.
Python
1def bonus(score):
2    print(score + 5)
3
4total = bonus(10) + 2
4
Give one benefit and one risk of using a library, and say what you should do before using code you found in an unknown place.