AQA 8525 GCSE Computer Science · 3.2 Programming

Stretch tasks Subroutines and Structured Programming

Task 1

Quiz Challenge

A quiz game against four computer players, then short and long written answers.

  1. Quiz Blitz12 questions against the clock
  2. Quick Fire4 short answers
  3. Big Think2 long answers

About 20 minutes

Task 2

This or That

Pixel the cat asks 8 questions. Each has two answers: pick the right one.

  • Two answers to every question
  • A fish treat for every right answer
  • Streaks win Pixel shades, then a party hat

About 4 minutes

Task 3

Memory Match

Flip the cards two at a time to match each term card to its meaning card.

  • 12 cards: each term has a meaning
  • One peek at every card
  • Combo points for pairs found in a row

6 pairs, about 4 minutes

Task 4

Sort It

Sort each statement into the right box, like the tick-box tables in the exam.

  • 10 cards to sort into 2 boxes
  • Drag a card, tap a box or press its number
  • Combo points for cards sorted in a row

10 cards, about 4 minutes

The games need JavaScript. Every question is below, with the answers at the end.

Read the lesson: Subroutines and Structured Programming

Subroutines and Structured Programming: the questions

Quiz Challenge, round 1: Quiz Blitz

  1. What is the difference between a procedure and a function?

    • There is no difference
    • A function returns a value; a procedure does not
    • A procedure returns a value; a function does not
    • Functions cannot take parameters
  2. In SUBROUTINE area(width, height), what are width and height?

    • Parameters
    • Arguments
    • Return values
    • Global variables
  3. In size ← area(4, 7), what are 4 and 7?

    • Records
    • Arguments
    • Parameters
    • Constants
  4. What does area(4, 7) return?

    SUBROUTINE area(width, height)
        RETURN width * height
    ENDSUBROUTINE

  5. A local variable exists only while its subroutine is running.

    • True
    • False
  6. What does tally() return the third time it is called?

    SUBROUTINE tally()
        count ← 0
        count ← count + 1
        RETURN count
    ENDSUBROUTINE

  7. What is the scope of a variable?

    • The size of the value it holds
    • Its data type
    • How long its name is
    • The part of the program where it can be accessed
  8. Which TWO are advantages of local variables?

    Tick 2.

    • They never need a value
    • They turn every subroutine into a function
    • Other subroutines cannot change them by accident
    • Their memory is freed when the subroutine ends
    • They can be used anywhere in the program
  9. What is structured programming?

    • Writing code without comments
    • Building a program from self-contained subroutines
    • Writing a program as one long block
    • Using only global variables
  10. Global variables are always wrong.

    • True
    • False
  11. How is a procedure called?

    • Only from the last line
    • With RETURN
    • As a statement on its own
    • Inside an assignment, as a value
  12. Which TWO are advantages of structured programming?

    Tick 2.

    • Each subroutine can be tested on its own
    • A change is made in one place
    • The program needs no testing
    • Every variable becomes global
    • It removes the need for parameters

Quiz Challenge, round 2: Quick Fire

  1. Explain the difference between a parameter and an argument. [2 marks]

  2. Explain why this subroutine returns 1 however many times it is called. [2 marks]

    SUBROUTINE tally()
        count ← 0
        count ← count + 1
        RETURN count
    ENDSUBROUTINE

  3. Give two reasons why local variables are a better default than global ones. [2 marks]

  4. Explain one advantage of structured programming, with a reason. [2 marks]

Quiz Challenge, round 3: Big Think

  1. Write a function in AQA pseudo-code called discount that takes a price and gives back the price with 10% taken off. Show how it would be called. [4 marks]

  2. Explain the advantages of structured programming. Give a reason for each. [6 marks]

This or That: tick the right answer

  1. Which kind of subroutine has a RETURN?

    • A function
    • A procedure
  2. Which variable can be changed from anywhere in the program?

    • A global variable
    • A local variable
  3. Two subroutines each use a local variable called i. What happens?

    • They stay completely separate
    • They overwrite each other
  4. When is a global variable a sensible choice?

    • Always, for every variable
    • When many parts share one value
  5. In greet('Amira'), what is 'Amira'?

    • A parameter
    • An argument
  6. Where does size ← area(4, 7) store the answer?

    • In size
    • In area
  7. An answer says structured code is easier to maintain. What earns the second mark?

    • The reason: a change is in one place
    • Saying it a second time
  8. A subroutine always works out 4 x 7 and nothing else. Is that useful?

    • No: parameters make it reusable
    • Yes: it never changes

Memory Match: match each term to its meaning

TermMeaning
A Procedure1 Exists only while its subroutine runs
B Function2 The value passed in a call
C Parameter3 Where a variable can be accessed
D Argument4 A name in the subroutine definition
E Local variable5 Returns a value
F Scope6 Does a task, returns nothing

Sort It: tick one box in each row

Local variable or global variable?

StatementLocalGlobal
Suits a running total shared by the whole game
Declared inside a subroutine
Its memory is freed when the subroutine ends
Keeps its value between subroutine calls
Can be accessed anywhere in the program
Declared outside every subroutine
A bug in it can come from anywhere
Keeps a subroutine self-contained
Two subroutines can each have one called i
Exists only while its subroutine runs
Answers

Round 1: Quiz Blitz

  1. A function returns a value; a procedure does not A function's call appears where a value is expected.
  2. Parameters Parameters are the names in the definition.
  3. Arguments Arguments are the actual values passed in when the subroutine is called.
  4. 28 4 x 7 = 28.
  5. True It is created when the subroutine starts and destroyed when it ends.
  6. 1 count is local, so it starts again at 0 on every call.
  7. The part of the program where it can be accessed Local variables have the scope of their subroutine. Globals can be reached anywhere.
  8. Other subroutines cannot change them by accident; Their memory is freed when the subroutine ends They also keep each subroutine self-contained.
  9. Building a program from self-contained subroutines Each subroutine has clearly defined inputs and outputs.
  10. False They suit genuinely shared values, such as a game's running total. Their danger is that anything can change them.
  11. As a statement on its own greet('Amira') stands alone. A function call sits where a value is needed.
  12. Each subroutine can be tested on its own; A change is made in one place It is also easier to write, read and reuse.

Round 2: Quick Fire

    • A parameter is the name in the subroutine's definition
    • An argument is the actual value passed in when it is called
    Model answer

    A parameter is the name listed in the subroutine's definition. An argument is the actual value passed in when the subroutine is called.

    • count is a local variable
    • It is created and set to 0 again on each call
    Model answer

    count is a local variable, so it is created and set to 0 each time the subroutine runs, and destroyed when it ends. It always goes from 0 to 1.

    • Other subroutines cannot change them by accident
    • Their memory is reclaimed when the subroutine ends
    • They keep the subroutine self-contained and reusable
    Model answer

    No other subroutine can change them by accident, and their memory is reclaimed as soon as the subroutine ends.

    • Easier to maintain, test, reuse or read
    • Because each task is in one self-contained subroutine
    Model answer

    It is easier to maintain, because a change only has to be made in the one subroutine responsible for that task, rather than everywhere the code was repeated.

Round 3: Big Think

    • A SUBROUTINE header with a parameter
    • RETURN the new price
    • Works out price * 0.9
    • Ends with ENDSUBROUTINE
    • Calls it with an argument
    Model answer
    AQA pseudo-code
    SUBROUTINE discount(price)
        RETURN price * 0.9
    ENDSUBROUTINE
    
    sale ← discount(50)
    • Easier to write: the problem is decomposed into small tasks
    • A team can work on different subroutines at the same time
    • Easier to test: each subroutine is tested on its own
    • A fault is found inside one small routine
    • Easier to maintain: a change is made in one place
    • Subroutines can be reused in other programs
    • Easier to read: the names explain the program
    Model answer

    Structured programming is easier to write, because decomposing the problem gives small tasks, and two people can work on different subroutines at the same time. It is easier to test, because each subroutine has defined inputs and can be tested on its own, so a fault is found inside one small routine. It is easier to maintain, because a change only has to be made in the one subroutine responsible, not everywhere the code was repeated. Subroutines can be reused in other programs, and a main program made of well-named calls is easier to read.

This or That

  1. A function A function returns a value. A procedure just carries out a task.
  2. A global variable That is what makes globals useful, and dangerous.
  3. They stay completely separate Each local i exists only inside its own subroutine.
  4. When many parts share one value A running total in a game is a fair use. Most values should be local.
  5. An argument It is the actual value passed in. The parameter is the name in the definition.
  6. In size The function returns 28, and the assignment stores it in size.
  7. The reason: a change is in one place Examiners want the reason, not just the label.
  8. No: parameters make it reusable Taking any two numbers as parameters lets it be called anywhere.

Memory Match

  • A 6 Procedure: Does a task, returns nothing
  • B 5 Function: Returns a value
  • C 4 Parameter: A name in the subroutine definition
  • D 2 Argument: The value passed in a call
  • E 1 Local variable: Exists only while its subroutine runs
  • F 3 Scope: Where a variable can be accessed

Sort It

  1. Global Suits a running total shared by the whole game Genuinely shared values are a fair use of a global.
  2. Local Declared inside a subroutine That makes it local to the subroutine.
  3. Local Its memory is freed when the subroutine ends A local variable stops using memory at the end.
  4. Global Keeps its value between subroutine calls It is not destroyed when a subroutine ends.
  5. Global Can be accessed anywhere in the program Global scope covers the whole program.
  6. Global Declared outside every subroutine That makes it global.
  7. Global A bug in it can come from anywhere Any subroutine can change a global.
  8. Local Keeps a subroutine self-contained Everything it uses lives inside it.
  9. Local Two subroutines can each have one called i Locals in different subroutines never clash.
  10. Local Exists only while its subroutine runs It is destroyed when the subroutine ends.