All subjects · Computer Science · WCP02 Practical Programming and Problem-solving

Designing programs: decomposition, scope and side effects

Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving

Good design produces correct results and code that is easy to change. The specification names the methods and practices examiners look for.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Methods to produce correct outcomes (spec 9.1.1)

Scope isolation and side effects (spec 9.1.2)

A local variable exists only inside its subprogram; a global one is visible everywhere. A side effect is any change a subprogram makes outside itself: changing a global, printing, writing a file, or changing a list passed in. Isolate scope by passing data in as parameters and returning results.

count = 0

def add_global():
    global count
    count = count + 1

def add_pure(n):
    return n + 1

add_global()
total = add_pure(5)
print(count, total)

def append_item(items):
    items.append(99)

data = [1, 2]
append_item(data)
print(data)

Output:

1 6
[1, 2, 99]

A list passed to a subprogram is the same list, so changes inside it are seen outside: a side effect. Copy the list first if the original must stay unchanged.

Other good design practice

Efficiency in practice

Finding the largest and smallest values in one loop does half the work of two separate loops:

readings = [14, 9, 22, 17, 5]
low = readings[0]
high = readings[0]
passes = 0
for r in readings:
    passes = passes + 1
    if r < low:
        low = r
    if r > high:
        high = r
print(low, high, passes)

Output:

5 22 5

Worked example

Decompose this task: 'Read pupils' marks from a file, reject marks outside 0 to 100, and display each valid pupil's grade in a table.'

  1. load_marks(filename): reads the file into a 2D list and returns it
  2. is_valid(mark): returns True if 0 <= mark <= 100
  3. grade(mark): returns the grade letter for a mark
  4. show_table(records): displays the formatted table
  5. The main program calls these in order and passes data between them as arguments and return values.

Answer: Four subprograms, each with one job, joined by parameters and return values

Key idea: Decompose into single-purpose subprograms; pass data in as parameters and out with return; avoid globals and hidden side effects; process data in as few passes as possible.

Check yourself

  1. What is displayed when this code runs? ```python x = 10 def change(): x = 50 return x y = change() print(x, y) ```
    Show answer

    10 50. Inside change(), x = 50 creates a new local variable; the global x is still 10: 10 50.

  2. What is displayed when this code runs? ```python x = 10 def change(): global x x = 50 change() print(x) ```
    Show answer

    50. global x makes the function change the global variable: 50.

  3. What is displayed when this code runs? ```python def double_all(values): for i in range(len(values)): values[i] = values[i] * 2 nums = [1, 2, 3] double_all(nums) print(nums) ```
    • [2, 4, 6]
    • [1, 2, 3]
    • None
    • [1, 2, 3, 2, 4, 6]
    Show answer

    [2, 4, 6]. The function receives the same list, so changing values changes nums: [2, 4, 6]. This is a side effect.

  4. What is displayed when this code runs? ```python def area(w, h): result = w * h return result a = area(3, 4) print(a) ```
    Show answer

    12. result is local to area(), but its value is returned and stored in a: 12.

  5. Which exception is raised when this code runs? ```python def setup(): limit = 5 setup() print(limit) ```
    • UnboundLocalError
    • NameError
    • TypeError
    • ValueError
    Show answer

    NameError. limit only exists inside setup(), so using it outside raises NameError.

  6. Which design shows the clearest separation of concerns?
    • One subprogram reads, calculates and displays everything
    • Every line is written in the main program with no subprograms
    • Each subprogram changes the same global variables
    • One subprogram reads the data, another calculates the totals, another displays them
    Show answer

    One subprogram reads the data, another calculates the totals, another displays them. Each part has one job, so it can be understood, tested and changed on its own.

  7. A program finds the total, the maximum and the minimum of 10 000 readings. Which approach is most efficient?
    • Three separate loops, one for each value
    • One loop that updates all three as it goes
    • Sort the list three times, then read the ends
    • Copy the list into three new lists first
    Show answer

    One loop that updates all three as it goes. A single pass visits each reading once, instead of three times, and needs no extra copies.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.