AQA 8525 GCSE Computer Science · 3.2 Programming
Stretch tasks Subroutines and Structured Programming
Quiz Challenge
A quiz game against four computer players, then short and long written answers.
- Quiz Blitz12 questions against the clock
- Quick Fire4 short answers
- Big Think2 long answers
About 20 minutes
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
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
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
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
In SUBROUTINE area(width, height), what are width and height?
- Parameters
- Arguments
- Return values
- Global variables
In size ← area(4, 7), what are 4 and 7?
- Records
- Arguments
- Parameters
- Constants
What does area(4, 7) return?
SUBROUTINE area(width, height) RETURN width * height ENDSUBROUTINEA local variable exists only while its subroutine is running.
- True
- False
What does tally() return the third time it is called?
SUBROUTINE tally() count ← 0 count ← count + 1 RETURN count ENDSUBROUTINEWhat 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
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
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
Global variables are always wrong.
- True
- False
How is a procedure called?
- Only from the last line
- With RETURN
- As a statement on its own
- Inside an assignment, as a value
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
Explain the difference between a parameter and an argument. [2 marks]
Explain why this subroutine returns 1 however many times it is called. [2 marks]
SUBROUTINE tally() count ← 0 count ← count + 1 RETURN count ENDSUBROUTINEGive two reasons why local variables are a better default than global ones. [2 marks]
Explain one advantage of structured programming, with a reason. [2 marks]
Quiz Challenge, round 3: Big Think
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]
Explain the advantages of structured programming. Give a reason for each. [6 marks]
This or That: tick the right answer
Which kind of subroutine has a RETURN?
- A function
- A procedure
Which variable can be changed from anywhere in the program?
- A global variable
- A local variable
Two subroutines each use a local variable called i. What happens?
- They stay completely separate
- They overwrite each other
When is a global variable a sensible choice?
- Always, for every variable
- When many parts share one value
In greet('Amira'), what is 'Amira'?
- A parameter
- An argument
Where does size ← area(4, 7) store the answer?
- In size
- In area
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
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
| Term | Meaning |
|---|---|
| A Procedure | 1 Exists only while its subroutine runs |
| B Function | 2 The value passed in a call |
| C Parameter | 3 Where a variable can be accessed |
| D Argument | 4 A name in the subroutine definition |
| E Local variable | 5 Returns a value |
| F Scope | 6 Does a task, returns nothing |
Sort It: tick one box in each row
Local variable or global variable?
| Statement | Local | Global |
|---|---|---|
| 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
- A function returns a value; a procedure does not A function's call appears where a value is expected.
- Parameters Parameters are the names in the definition.
- Arguments Arguments are the actual values passed in when the subroutine is called.
- 28 4 x 7 = 28.
- True It is created when the subroutine starts and destroyed when it ends.
- 1 count is local, so it starts again at 0 on every call.
- The part of the program where it can be accessed Local variables have the scope of their subroutine. Globals can be reached anywhere.
- Other subroutines cannot change them by accident; Their memory is freed when the subroutine ends They also keep each subroutine self-contained.
- Building a program from self-contained subroutines Each subroutine has clearly defined inputs and outputs.
- False They suit genuinely shared values, such as a game's running total. Their danger is that anything can change them.
- As a statement on its own greet('Amira') stands alone. A function call sits where a value is needed.
- 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 answerA 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 answercount 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 answerNo 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 answerIt 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 answerAQA pseudo-codeSUBROUTINE 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 answerStructured 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
- A function A function returns a value. A procedure just carries out a task.
- A global variable That is what makes globals useful, and dangerous.
- They stay completely separate Each local i exists only inside its own subroutine.
- When many parts share one value A running total in a game is a fair use. Most values should be local.
- An argument It is the actual value passed in. The parameter is the name in the definition.
- In size The function returns 28, and the assignment stores it in size.
- The reason: a change is in one place Examiners want the reason, not just the label.
- 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
- Global Suits a running total shared by the whole game Genuinely shared values are a fair use of a global.
- Local Declared inside a subroutine That makes it local to the subroutine.
- Local Its memory is freed when the subroutine ends A local variable stops using memory at the end.
- Global Keeps its value between subroutine calls It is not destroyed when a subroutine ends.
- Global Can be accessed anywhere in the program Global scope covers the whole program.
- Global Declared outside every subroutine That makes it global.
- Global A bug in it can come from anywhere Any subroutine can change a global.
- Local Keeps a subroutine self-contained Everything it uses lives inside it.
- Local Two subroutines can each have one called i Locals in different subroutines never clash.
- Local Exists only while its subroutine runs It is destroyed when the subroutine ends.