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.
Worked examples
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.
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-routine
Parameter
Returns a value?
Can be used in a sum?
double(n): return n * 2
n
yes
yes
welcome(name): print("Hi", name)
name
no
no
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?
Position
1
2
3
4
5
6
Item
compass
eraser
stapler
tape
marker
scissors
(a) 4 looks, found at position 4. (b) 1 look, found at position 1. (c) 6 looks, because every item is checked, and it reports Not found.
2
What does this program print? Show the value returned by each call.
Python shows TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'. The function prints 15 but does not return a value, so bonus(10) has no value to add 2 to. Change print(score + 5) to return score + 5, and print the total.
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.
Benefit: ready-made code saves time and has often been tested. Risk: code from an untrusted place may have mistakes or may be harmful. Before using it, ask a trusted adult to check that the source can be trusted.