Sub Programs
A procedure is a sub program that carries out a task and does not return a value to the calling code. You define it once, then call it whenever that task is needed, but nothing comes back from the call — no number, no text, not even True or False.
Because a procedure sends no value back, a call does not produce a result you can use in a calculation or store for later use. Procedure calls are written as statements on their own line. If the calling code needs a value returned, a function is used instead.
def showLap(lapNumber, seconds):
print("Lap " + str(lapNumber) + " took " + str(seconds) + " seconds")
showLap(1, 63)
showLap(2, 58)Output:
Lap 1 took 63 seconds
Lap 2 took 58 seconds
Here showLap takes two parameters, so the same procedure can report a different lap each time it runs, yet it still returns nothing — it simply prints. The parameters supply the data the task works on, which is what makes one procedure reusable for every lap instead of a separate block of code for each.
OCR GCSE Computer Science · Sub Programs