Question bank › A-level › A-level Computer Science › Fundamentals of programming

Subroutines, parameters, stack frames and recursion

10 questions46 marksabout 55 min

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

1Standard5 marks

The recursive function F is shown below.

FUNCTION F(a, b)
   IF b = 0 THEN
      RETURN a
   ELSE
      RETURN F(b, a MOD b)
   ENDIF
ENDFUNCTION

(a) State the value returned by the call F(84, 36). [1]

(b) State how many times F is called in total, including the original call, when F(84, 36) is executed. [1]

(c) State what the function F calculates. [1]

(d) Identify the base case in F and explain why a recursive subroutine must have a base case. [2]

2Hard4 marks

Consider the Python program below.

def change(x, items):
    x = x + 1
    items.append(x)

a = 5
b = [1]
change(a, b)
print(a, b)

(a) State the output of the program. [2]

(b) Explain why the value of a and the contents of b are affected differently by the call to change. [2]

3Core3 marks

A programmer is writing a large program to manage bookings for a sports centre. She decides to break the program up into subroutines.

(a) Explain what is meant by a subroutine. [1]

(b) Give two advantages of using subroutines when developing this program. [2]

Stuck on Subroutines, parameters, stack frames and recursion?

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.

4Core4 marks

The subroutine Discount is shown below, written in pseudo-code.

SUBROUTINE Discount(price, percent)
   saving ← price * percent / 100
   RETURN price - saving
ENDSUBROUTINE

cost ← Discount(80, 15)
OUTPUT cost

(a) Identify a parameter used in Discount. [1]

(b) Identify a local variable used in Discount. [1]

(c) State the value that is output by the program. [1]

(d) Explain what is meant by the interface of a subroutine. [1]

5Standard5 marks

Part of a program is shown below. Line numbers are included for reference.

1  SUBROUTINE AddVAT(amount)
2     vat ← amount * 0.2
3     RETURN amount + vat
4  ENDSUBROUTINE
5
6  SUBROUTINE CalcTotal(price, qty)
7     subtotal ← price * qty
8     total ← AddVAT(subtotal)
9     RETURN total
10 ENDSUBROUTINE

The main program calls CalcTotal(12, 3). The diagram shows the contents of the call stack immediately after line 2 of AddVAT has been executed.

(a) State what is stored in the location labelled X. [1]

(b) Explain why a stack is a suitable data structure for storing the information needed for subroutine calls. [2]

(c) Describe what happens to the call stack, and how execution continues, when AddVAT finishes executing. [2]

Call stack ← top of stack Stack frame for AddVAT X parameter: amount = 36 local variable: vat = 7.2 Stack frame for CalcTotal return address (in main) parameter: price = 12 parameter: qty = 3 local variable: subtotal = 36 bottom of stack
6Standard5 marks

The recursive subroutine Show is shown below. DIV gives the whole-number part of a division and MOD gives the remainder.

SUBROUTINE Show(n)
   IF n > 1 THEN
      Show(n DIV 2)
   ENDIF
   OUTPUT n MOD 2
ENDSUBROUTINE

(a) State the output when the call Show(13) is made. Assume each OUTPUT is displayed on the same line with no spaces. [2]

(b) State how many times Show is called in total, including the original call, when Show(13) is executed. [1]

(c) State the purpose of the subroutine Show. [1]

(d) Explain why the digit output first is produced by the last call to Show that was made. [1]

7Hard5 marks

A function CountChar is needed that takes two parameters: a string s and a single character c. It must return the number of times c appears in s. For example, CountChar("banana", "a") returns 3.

Write a recursive version of CountChar, using pseudo-code or a programming language you have studied. You must not use any loop or built-in counting function. You may use LEN(s), s[0] for the first character and SUBSTRING(1, LEN(s) - 1, s) (or an equivalent such as s[1:]) for all characters after the first. [5]

8Hard5 marks

The recursive subroutine Total is shown below.

SUBROUTINE Total(n)
   IF n = 0 THEN
      RETURN 0
   ELSE
      RETURN n + Total(n - 2)
   ENDIF
ENDSUBROUTINE

(a) Explain what will happen when the call Total(7) is made. [2]

(b) On one computer, 12 000 bytes of the stack are available for calls to Total, and each stack frame for Total occupies 48 bytes. Calculate the largest even value of n for which Total(n) can complete without running out of stack space. Show your working. [2]

(c) State a change to the base case so that Total works correctly for odd values of n. [1]

9Stretch5 marks

A staircase has n steps. A person can climb either 1 or 2 steps at a time. The recursive function Ways returns the number of different ways of climbing the staircase.

SUBROUTINE Ways(n)
   IF n <= 2 THEN
      RETURN n
   ELSE
      RETURN Ways(n - 1) + Ways(n - 2)
   ENDIF
ENDSUBROUTINE

(a) State the value returned by Ways(6). [1]

(b) State the total number of calls to Ways, including the original call, made when Ways(6) is executed. [1]

(c) State the maximum number of stack frames for Ways that are on the call stack at the same time during the execution of Ways(6). [1]

(d) Explain why this recursive function is inefficient for large values of n, and describe how it could be improved. [2]

10Standard5 marks

Consider the Python program below.

total = 10

def add_points(p):
    total = p * 2
    return total

def add_bonus():
    global total
    total = total + 5

print(add_points(3))
print(total)
add_bonus()
print(total)

(a) State the three values output by the program, in order. [3]

(b) Explain two reasons why it is good practice to use local variables rather than global variables in subroutines. [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