Skip to content
IGCSE·Tuition
Computer Science · Lessons

Trace a returned value

The function looks short and you can read every line, yet your final answer still comes out wrong.

On this page
  1. What is the method for tracing a function?
  2. Worked example
  3. Which mistake catches students most often?
  4. Self-check
  5. Where does this lead next?

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?

  1. Copy the call’s arguments into the parameters. Write them in the first row.
  2. Set up one column per variable. Include parameters, locals and the loop counter.
  3. Run each line in order. Write a new row whenever a value changes.
  4. Stop at the first RETURN that runs. The returned value is what that RETURN holds at that moment.
  5. 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.

StepABIResult
Call43not setnot set
Result ← 043not set0
I = 14310 + 4 = 4
I = 24324 + 4 = 8
I = 34338 + 4 = 12
RETURN Result12

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
ENDFUNCTION

The 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.

Questions people ask

What does RETURN actually do?

RETURN ends the function immediately and sends one value back to the line that called it. Any lines after RETURN do not run. The calling code can then store the value, output it, or use it in a calculation.

Why must I write a trace table when I can see the answer?

Short functions hide small slips, such as a loop running one time too few. A table forces you to record each variable after each step, so the answer comes from the code and not from a guess. Examiners also award marks for correct intermediate values.

What happens if the function never reaches a RETURN?

A function that is meant to return a value should return on every possible path. If a path has no RETURN, the caller receives no value, which is a logic error. Check every IF branch when you read a function.

Updated:

Your next step

If your traces go wrong halfway down a loop, a one-to-one teacher can watch you fill in the table live and stop you at the exact row where the value drifts.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80. Other fees, schedules and ongoing arrangements are confirmed directly with your teacher after the trial class.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parent or guardian? Enquire here

9,000+ students helped through our service