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)[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)[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")Plan your program here, then type it in and press Run.
survey_copy() that makes its own list and returns it. Which version is easier to test, and why?survey, add readings = [] as the first line. What does the main program see now, and why?nearest_two(readings) that returns the indexes of the two smallest readings.