Question bank › GCSE › GCSE Computer Science › Programming

Structured programming & subroutines

10 questions44 marksabout 55 min

Answer on paper first, then reveal the mark scheme one mark at a time.

1Grade 6–76 marks

Look at this subroutine.

SUBROUTINE calcPrice(price, member)
   IF member = True THEN
      RETURN price * 0.9
   ELSE
      RETURN price
   ENDIF
ENDSUBROUTINE

(a) State one parameter used in the subroutine. [1]

(b) State the value returned by calcPrice(40, True). [1]

(c) Explain the difference between a local variable and a global variable. [2]

(d) Give two advantages of using subroutines in a program. [2]

2Grade 1–33 marks

Part of a program is shown below.

SUBROUTINE showTicket(name, seat)
   OUTPUT "Ticket for " + name
   OUTPUT "Seat: " + seat
ENDSUBROUTINE

showTicket("Ava", "B12")

(a) State the name of the subroutine. [1]

(b) State how many parameters the subroutine has. [1]

(c) Describe what the line showTicket("Ava", "B12") does. [1]

3Grade 4–54 marks

A subroutine is shown below.

SUBROUTINE toSeconds(mins, secs)
   RETURN mins * 60 + secs
ENDSUBROUTINE

The main program then runs these lines.

a ← toSeconds(2, 15)
b ← toSeconds(0, 45)
OUTPUT a
OUTPUT a + b

(a) State the value stored in a. [1]

(b) State what is output by the line OUTPUT a + b. [1]

(c) Explain why the subroutine toSeconds needs a RETURN statement in this program. [2]

Stuck on Structured programming & subroutines?

Get it sorted in a free 30-minute session

We work through your weakest questions on this topic on Zoom. I reply the same day.

4Grade 4–54 marks

A program is shown below. The line numbers are not part of the program.

1  count ← 10
2  SUBROUTINE addOne()
3     count ← 5
4     count ← count + 1
5     RETURN count
6  ENDSUBROUTINE
7  OUTPUT addOne()
8  OUTPUT count

In this language, a variable assigned inside a subroutine is a local variable.

(a) State the output of line 7. [1]

(b) State the output of line 8. [1]

(c) Explain why the outputs of lines 7 and 8 are different. [2]

5Grade 6–75 marks

A games program needs a subroutine called maxOfThree that takes three integers as parameters and returns the largest of the three. If two or more of the values are equal largest, that value is returned.

For example, maxOfThree(3, 9, 4) returns 9 and maxOfThree(7, 7, 2) returns 7.

Write the subroutine maxOfThree using either pseudo-code or a programming language you have studied. You must not use a built-in function that finds the maximum. [5]

6Grade 6–74 marks

Two subroutines from a program are shown below.

SUBROUTINE countStars(n)
   s ← ""
   FOR i ← 1 TO n
      s ← s + "*"
   ENDFOR
   RETURN s
ENDSUBROUTINE

SUBROUTINE printStars(n)
   FOR i ← 1 TO n
      OUTPUT "*"
   ENDFOR
ENDSUBROUTINE

(a) State what is output by OUTPUT countStars(3) + countStars(2). [1]

(b) Explain why the statement line ← printStars(5) would not work as intended. [2]

(c) The variable i is used in both subroutines. Explain why this does not cause a problem. [1]

7Grade 8–96 marks

A shop program adds 20% VAT to prices. Part of the program is shown below.

price1 ← 50
price1 ← price1 * 1.2
OUTPUT "Total: " + REAL_TO_STRING(price1)

price2 ← 20
price2 ← price2 * 1.2
OUTPUT "Total: " + REAL_TO_STRING(price2)

price3 ← 12.5
price3 ← price3 * 1.2
OUTPUT "Total: " + REAL_TO_STRING(price3)

(a) The programmer decides to use a structured approach and move the repeated code into a subroutine called showTotal that takes a price as a parameter and outputs the total. Write the subroutine showTotal and the three lines of main program that replace the code above. [4]

(b) Explain one reason why the new version of the program is easier to maintain. [2]

8Grade 4–53 marks

A team of three programmers is writing a large program. Before coding, they agree the name, the parameters and the return value of every subroutine and write this down in a shared document.

(a) State the term used for the agreed name, parameters and return value of a subroutine. [1]

(b) Explain how agreeing this document before coding helps the team. [2]

9Grade 6–74 marks

A subroutine is shown below. DIV is integer division and MOD gives the remainder.

SUBROUTINE digitSum(n)
   total ← 0
   WHILE n > 0
      total ← total + (n MOD 10)
      n ← n DIV 10
   ENDWHILE
   RETURN total
ENDSUBROUTINE

(a) Complete the trace table for the call digitSum(305). Record a value only when it changes. [3]

ntotal
3050

(b) The main program contains the line OUTPUT total after calling digitSum(305). Explain why this line would cause an error. [1]

10Grade 8–95 marks

A programmer writes a subroutine to add up all the values in an array.

total ← 0

SUBROUTINE addUp(arr)
   FOR i ← 0 TO LEN(arr) - 1
      total ← total + arr[i]
   ENDFOR
   RETURN total
ENDSUBROUTINE

OUTPUT addUp([4, 6])
OUTPUT addUp([1, 2, 3])

In this language total is a global variable because it is declared outside the subroutine.

(a) State the two values output by the program. [1]

(b) Explain why the second output is incorrect. [2]

(c) Describe how the programmer should change the subroutine so that it gives the correct answer every time it is called, and give the type of error that has been corrected. [2]

Stuck on this topic?

Book a free 30-minute session on WhatsApp and we'll find exactly which step is costing you the marks. Or build a timed paper on it.

Free 30-minute session