Can one procedure print every runner’s result at a Leeds school sports day?
At a school sports day in Leeds, your program can print Amira’s race result. Then the next runner finishes. Can you use the same procedure to print a different name and time without rewriting it?
You already know how to give a block of code a name. Here, the call to show result runs the print line inside it. The name and time are written into that line, so every call prints Amira’s result.
This time, the call supplies Amira first and fourteen second. Watch where they go. Amira goes into runner, the first slot. Fourteen goes into seconds, the second slot. The procedure uses those two values to print the result.
The procedure expects runner first and seconds second. This call gives Ben first and sixteen second. Which pair of values will the procedure use? Pick an answer.
Look at the definition first. Runner and seconds are parameters, the names in the procedure header. Now look at the call. Amira and fourteen are arguments. In these calls, position tells you which argument goes with which parameter.
First, look at the print line. It turns the time into text so it can join it to the name and units. Call with Amira and fourteen. Match them in order and read Amira, colon, fourteen seconds. Now call with Ben and sixteen. Match them in order and read Ben, colon, sixteen seconds.
In the caller, athlete holds Nia and time holds thirteen. The call passes athlete first and time second. What does show result print? Pick the exact output.
Both columns start with the caller’s variables, athlete and time. The mistake is thinking the procedure needs those same names. Look across to the trace on the right. Nia goes into runner because athlete is first, and thirteen goes into seconds because time is second.
The parameters and the variable line are local to format result. You can use those names inside the function, and each call has its own local names until it ends. That keeps them separate from the caller’s names. Return sends the text back. The caller stores it in result. Nothing is printed.
The comments document the interface: supply a name, then a time in seconds, and the procedure prints one line. It uses format result to build the text. If an exam asks for an advantage of modular programming, explain how splitting these jobs lets you change the text format without editing the calls.