Skip to content
SPM Tuition
Computer Science · Programming foundations

Tracing functions and parameters

You lose track of which value goes where when a program calls a function.

A function takes values in through parameters, does some work and sends one value back with RETURN. To trace a call, match each argument to its parameter by position, run the body, then replace the call with the returned value.

This lesson is part of programming foundations. It builds on writing loops with correct boundaries, because functions can contain loops.

How do you trace a function call?

Use three moves.

  1. Match: copy each argument into the parameter in the same position.
  2. Run: follow the body line by line using those parameter values.
  3. Return: put the returned value where the call was written, and carry on.

Write the parameter values at the top of your trace table, so you never mix them with the caller’s variables.

Worked example: area of a rectangle

FUNCTION area(length, width)
  RETURN length * width
END FUNCTION

SET w = 3
SET result = area(w + 2, 4)
OUTPUT result
Move Working
Evaluate arguments w + 2 = 5, and 4
Match length = 5, width = 4
Run and return 5 × 4 = 20
Store result = 20

The output is 20. Notice that w stays 3 in the caller, because only its value was used.

What mistake do students make here?

The usual mistake is to match arguments by name or to swap their order. Consider a function that finds a remainder.

FUNCTION remainder(a, b)
  RETURN a MOD b
END FUNCTION
Call a b Result
remainder(17, 5) 17 5 17 MOD 5 = 2
remainder(5, 17) 5 17 5 MOD 17 = 5

Same two numbers, different answer. The position of each argument decides which parameter receives it, so always write the match step.

Check yourself

Trace this and state the output.

FUNCTION bigger(p, q)
  IF p > q THEN
    RETURN p
  ELSE
    RETURN q
  END IF
END FUNCTION

OUTPUT bigger(7, 12) + bigger(9, 3)
Answer

For bigger(7, 12): p = 7, q = 12, 7 > 12 is false, so it returns 12. For bigger(9, 3): p = 9, q = 3, 9 > 3 is true, so it returns 9.

The expression becomes 12 + 9, so the output is 21.

What to study next

Next, see how to locate faults in whole programs in program structure and debugging. You can practise call tracing with the restricted pseudocode trace trainer.

If you want a teacher to go through your function traces, see online one-to-one Computer Science tuition.

Common questions

What is the difference between a parameter and an argument?

A parameter is the name in the function definition, such as length. An argument is the actual value given when the function is called, such as 5. The call copies each argument into the parameter in the same position.

What does RETURN do?

It sends a value back to the place where the function was called and ends the function. The calling line then uses that value as if it were written there.

Does changing a parameter inside a function change the variable outside?

In the pseudocode normally used at this level, the function works on a copy of the value. The original variable in the caller keeps its value unless the function returns a result and you store it.

Does the order of arguments matter?

Yes. Arguments are matched to parameters by position, not by name. Swapping two arguments can produce a completely different result, which is a common source of wrong answers.

If function calls make your trace tables fall apart, a one-to-one Computer Science teacher can walk through your own examples and keep the arguments and parameters straight.

  • Online one-to-one lessons for your child with an experienced teacher.
  • Your first class is a one-hour trial, from RM50. The fee is agreed before you book.
  • Happy with the teacher? Continue with lessons of about 1.5 hours. If not, ask for another teacher.