AQA GCSE Computer Science June 2024 Paper 1, Question 10: subroutines that call subroutines

AQA 8525 June 2024 Paper 1, Question 10: work out what is printed when First calls Second and Second calls Third, for the values 3, 4, 4 and then 3, 4, 8. The parameter order trap, both answers worked through, and the program to run.

Past paper questionAQA 8525/1BJune 2024 Paper 12 marksRead the code

Question 10 of the AQA GCSE Computer Science Paper 1 sat on 15 May 2024 (8525/1B, the Python paper) is only 2 marks, and it is easy to get wrong. Three subroutines call each other, and every one of them uses the names p1 and v1 for something different.

We do not copy the exam paper here. Open it beside this page: AQA June 2024 Paper 1B question paper (PDF). When you have finished, check the mark scheme too.

The question in short

  • First(p1, p2, p3) adds p2 and p3 to make v1, then prints Second(v1, p1).
  • Second(p1, p2) adds its two parameters to make v1. If v1 is greater than 12, it adds Third(p1) to v1. It returns v1.
  • Third(p1) returns 2 if p1 is greater than 3, and 0 otherwise.

The thing to get straight

Parameters are matched by position, not by name. First calls Second(v1, p1). So inside Second, the name p1 holds what First called v1, and p2 holds what First called p1. Each subroutine has its own private set of names.

Write the values down afresh each time you enter a subroutine.

Part 1: First(3, 4, 4)

In First: p1 = 3, p2 = 4, p3 = 4. v1 = 4 + 4 = 8. It calls Second(8, 3).

In Second: p1 = 8, p2 = 3. v1 = 8 + 3 = 11. Is 11 greater than 12? No. It returns 11.

The output is 11.

Part 2: First(3, 4, 8)

In First: v1 = 4 + 8 = 12. It calls Second(12, 3).

In Second: p1 = 12, p2 = 3. v1 = 15. Is 15 greater than 12? Yes, so it calls Third(12).

In Third: p1 = 12. Is 12 greater than 3? Yes. It returns 2.

Back in Second: v1 = 15 + 2 = 17. It returns 17.

The output is 17.

Where the marks are lost

  • Passing the wrong value to Third. Second calls Third(p1) with its own p1, which is 12, not the 3 that First called p1. With 3, Third would return 0 and the answer would be 15.
  • Adding all three numbers. 3 + 4 + 4 = 11 happens to give the first answer, and 3 + 4 + 8 = 15 does not give the second.
  • Forgetting that Second returns and First prints. Only one thing is printed, once.

Run it

The program with a print at the start of every subroutine, so that you can see which values arrive where. Change the call at the bottom.

First(3, 4, 4) prints 11 and never reaches Third. First(3, 4, 8) prints 17, and shows Third receiving 12.
The program
from bugbot import *
connect()

def First(p1, p2, p3):
    print("First has", p1, p2, p3)
    v1 = p2 + p3
    print(Second(v1, p1))

def Second(p1, p2):
    print("  Second has", p1, p2)
    v1 = p1 + p2
    if v1 > 12:
        v1 = v1 + Third(p1)
    return v1

def Third(p1):
    print("    Third has", p1)
    if p1 > 3:
        return 2
    else:
        return 0

First(3, 4, 4)
First(3, 4, 8)
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

Questions

What are the answers to AQA GCSE Computer Science 2024 Paper 1 Question 10?

11 for the values 3, 4 and 4. 17 for the values 3, 4 and 8.

How are arguments matched to parameters?

By position. The first argument in the call goes to the first parameter in the definition, the second to the second, and so on. The names used in the calling code do not matter.

Can two subroutines use the same variable name?

Yes. Parameters and variables made inside a subroutine are local to it, so p1 in one subroutine is a different variable from p1 in another.

More from this paper

Every AQA 8525 question we have worked

Learn it step by step

  1. F4.2 Parameters and return values Functions and structured code
  2. F4.3 Local and global variables Functions and structured code
Open the lessons

This is our own explanation of a published exam question. It is not written or endorsed by AQA, and the question paper and mark scheme remain AQA's copyright. Read them on AQA's site with the links on this page.