Programming techniques and object-oriented programming · A level · OCR H446 2.2.1, AQA 7517 4.1.1.10, Eduqas A500QS 1.4 · about 20 min
Out-of-line subroutines and their interfaces, returning several values, and passing by value and by reference.
[1 mark]What does it mean for a subroutine to be out of line?
[1 mark]A procedure is called with an argument passed by reference, and changes its parameter. What happens to the caller's variable?
[1 mark]What does this program print?
def nudge(n, items):
n = n + 1
items.append(n)
count = 5
log = []
nudge(count, log)
nudge(count, log)
print(count, log)5 [6, 6]
Each call makes n a new local 6, so count stays 5, but append changes the one list that log and items both refer to.
[1 mark]What does this program print?
def stats(values):
low = values[0]
high = values[0]
for v in values:
if v < low:
low = v
if v > high:
high = v
return low, high
low, high = stats([31, 15, 51, 20])
print(high - low)36
The function returns two values as a tuple; 51 - 15 is 36.
[1 mark]Which are advantages of passing a large array by reference rather than by value?
Tick every answer that is true.
[1 mark]What is the interface of a subroutine?
Write two subroutines.
- The procedure survey(readings) takes an empty list readings. It takes eight distance readings, turning right 45 degrees after each one, so the robot ends facing where it started, and appends each reading to readings. It returns nothing, and the main program calls it on a line of its own, survey(readings).
- The function nearest(readings) takes a list of real numbers and returns two values: the index of the smallest reading (the first one if two are equal) and that reading. Find it with a loop; do not use min, index or global.
Then print nearest: <cm> cm at <degrees> degrees, where the degrees are the index times 45, turn right by that many degrees to face the nearest wall, and play one note.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
def survey(readings):
pass
def nearest(readings):
return 0, 0.0
readings = []
survey(readings)
index, cm = nearest(readings)
print(f"nearest: {cm} cm at {index * 45} degrees")The hint students can ask for: A list passed to a subroutine is the same list the caller has, so the procedure can fill it without returning anything. For the nearest, keep the best index and the best distance so far as you go through the list, and hand both back at the end.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
def survey(readings):
for i in range(8):
readings.append(distance())
turn_right(30, angle=45)
def nearest(readings):
best = 0
for i in range(1, len(readings)):
if readings[i] < readings[best]:
best = i
return best, readings[best]
readings = []
survey(readings)
index, cm = nearest(readings)
print(f"nearest: {cm} cm at {index * 45} degrees")
turn_right(30, angle=index * 45)
tone(880, 0.3)
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.