Tracing a returned value means following a function call line by line until RETURN, recording what each variable holds, and stating the value that goes back to the caller. Exam questions give a function and a call, then ask for the value returned or for the output.
This lesson is part of the programming structure module. It uses the parameters from building a subroutine and the local variables from scope.
What is the method for tracing a function?
- Copy the call’s arguments into the parameters. Write them in the first row.
- Set up one column per variable. Include parameters, locals and the loop counter.
- Run each line in order. Write a new row whenever a value changes.
- Stop at the first RETURN that runs. The returned value is what that RETURN holds at that moment.
- Say what the caller does with it, such as outputting it.
Worked example
Trace this program and state the output.
FUNCTION RepeatAdd(A : INTEGER, B : INTEGER) RETURNS INTEGER
DECLARE Result : INTEGER
Result ← 0
FOR I ← 1 TO B
Result ← Result + A
NEXT I
RETURN Result
ENDFUNCTION
OUTPUT RepeatAdd(4, 3)
The call RepeatAdd(4, 3) gives A = 4 and B = 3. The loop runs for I = 1, 2 and 3.
| Step | A | B | I | Result |
|---|---|---|---|---|
| Call | 4 | 3 | not set | not set |
| Result ← 0 | 4 | 3 | not set | 0 |
| I = 1 | 4 | 3 | 1 | 0 + 4 = 4 |
| I = 2 | 4 | 3 | 2 | 4 + 4 = 8 |
| I = 3 | 4 | 3 | 3 | 8 + 4 = 12 |
| RETURN Result | 12 |
The function returns 12, and the main program outputs 12. The function repeats addition B times, so it works out A × B. As a check, 4 × 3 = 12.
Which mistake catches students most often?
Mistaken code:
FUNCTION SumTo(N : INTEGER) RETURNS INTEGER DECLARE Total : INTEGER Total ← 0 FOR I ← 1 TO N Total ← Total + I RETURN Total NEXT I ENDFUNCTIONThe writer expects
SumTo(4)to return 1 + 2 + 3 + 4 = 10.
The RETURN sits inside the loop. On the first pass I = 1, Total becomes 1, and RETURN ends the function straight away. So SumTo(4) returns 1.
The correction is to move RETURN after NEXT, outside the loop. Then the loop finishes all four passes, Total reaches 1 + 2 + 3 + 4 = 10, and 10 is returned.
A second mistake is calling a function and ignoring the value. Writing RepeatAdd(4, 3) on a line by itself does nothing useful. Store it, for example Answer ← RepeatAdd(4, 3), or output it.
Self-check
1. State the value returned by RepeatAdd(5, 0).
Show answer
B is 0, so the loop FOR I ← 1 TO 0 does not run at all. Result stays 0 and 0 is returned.
2. State the value returned by RepeatAdd(3, 4).
Show answer
Result goes 0, then 3, 6, 9, 12 over four passes. It returns 12.
3. State the output.
FUNCTION Larger(X : INTEGER, Y : INTEGER) RETURNS INTEGER
IF X > Y THEN
RETURN X
ELSE
RETURN Y
ENDIF
ENDFUNCTION
OUTPUT Larger(7, 12)
Show answer
X = 7 and Y = 12. The test 7 > 12 is FALSE, so the ELSE branch runs and returns Y. The output is 12.
Where does this lead next?
Now that you can trace what a function sends back, think about what a function should and should not do besides returning. That is the subject of separating logic from input and output. Practise with the restricted pseudocode trace trainer, which shows each row of the table, and compare with the safe Python reasoning sandbox.
Students who can trace but lose time in the exam often need a routine, and an online one-to-one Computer Science teacher can build one around your own papers.