AQA GCSE CS · 3.2.10 to 3.2.11
Subroutines and Structured Programming
Name
Class
Date
Mark / 24
Section A
Circle the letter of the correct answer. One mark each.
- 1What is the difference between a procedure and a function?1
- AA function is longer than a procedure
- BA procedure runs faster
- CA procedure can take parameters and a function cannot
- DA function returns a value and a procedure does not
- 2In SUBROUTINE area(width, height), what are width and height?1
- AGlobal variables
- BArguments
- CParameters
- DReturn values
- 3A variable declared inside a subroutine is described as having what kind of scope?1
- APublic
- BGlobal
- CStatic
- DLocal
- 4Why can two different subroutines both use a local variable called count?1
- AThe compiler renames one of them
- BEach exists only within its own subroutine, so they do not interfere
- CLocal variables are stored in different files
- DOnly one of them actually works
- 5Which of these is a genuine advantage of structured programming?1
- AA fault in one task only needs correcting in the subroutine responsible for it
- BPrograms written this way always run faster
- CIt means variables never need names
- DIt removes the need for testing
- 6A subroutine returns 1 every time it is called, even though it should count upwards. What is the most likely cause?1
- AThe subroutine is a procedure
- BThe parameter is missing
- CThe counter is a local variable that is re-created each call
- DThe return statement is in the wrong place
- 7What is passed to a subroutine when it is called?1
- ARecords
- BParameters
- CFields
- DArguments
- 8Which statement about global variables is correct?1
- AAny part of the program can change one, which makes some bugs hard to trace
- BThey use less memory than local variables
- CThey are destroyed when a subroutine ends
- DThey cannot be used in structured programs
- 9Why does using parameters make a subroutine more useful?1
- AIt can be called with different values rather than working on one fixed case
- BIt runs faster
- CIt removes the need for a return value
- DIt uses less memory
- 10A program calls validate(age) and stores the result. What must validate be?1
- AA global variable
- BA function, because its result is stored
- CA procedure, because it takes a parameter
- DA record
Section B
Answer in the space provided. The number of marks tells you how many separate points to make.
- 1StateState one difference between a parameter and an argument.2
- 2ExplainExplain why it is usually better to use local variables than global variables inside a subroutine.4
- 3DescribeDescribe two advantages of breaking a long program into subroutines.4
- 4ExplainA subroutine is written to count how many times it has been called, but it returns 1 every time. Explain the cause and how it could be corrected.4
Mark scheme
Section A
Section B
1 State one difference between a parameter and an argument. 2
One mark for each of the following, to a maximum of 2.
- A parameter is the name given in the subroutine definition
- An argument is the actual value passed in when the subroutine is called
Model answer. A parameter is the named variable listed in the subroutine's definition, which receives a value when the subroutine runs. An argument is the actual value supplied at the point where the subroutine is called.
2 Explain why it is usually better to use local variables than global variables inside a subroutine. 4
One mark for each of the following, to a maximum of 4.
- A local variable exists only while the subroutine runs
- So it cannot be changed by any other part of the program
- Two subroutines can use the same variable name without conflict
- The subroutine becomes self contained, so it is easier to test and reuse
Model answer. A local variable is created when the subroutine starts and destroyed when it finishes, so it cannot be read or altered by any other part of the program. That removes a whole class of bug in which one section of a program changes a value another section was relying on, and it means two subroutines can each use a variable called total without either affecting the other. It also makes the subroutine self contained: everything it needs arrives as a parameter and everything it produces leaves as a return value, so it can be tested on its own and reused in another program without dragging the rest of the code along with it.
3 Describe two advantages of breaking a long program into subroutines. 4
One mark for each of the following, to a maximum of 4.
- Code written once can be called from several places, so there is less repetition
- A change or correction only has to be made in one place
- Each subroutine can be tested independently
- The program is easier to read and several people can work on it at once
Model answer. The first advantage is maintenance. A task written once as a subroutine is called wherever it is needed, so there is no repeated code, and when that task has to change it is corrected in exactly one place rather than in every copy. The second is testing and readability. Each subroutine has one clear job and defined inputs and outputs, so it can be tested on its own with a small set of values before it is trusted, and the main program reads as a list of named steps, which makes it far easier for another programmer, or for you in six months, to follow.
4 A subroutine is written to count how many times it has been called, but it returns 1 every time. Explain the cause and how it could be corrected. 4
One mark for each of the following, to a maximum of 4.
- The counter is declared as a local variable inside the subroutine
- It is therefore created again each time the subroutine is called
- The value from the previous call is lost when the subroutine ends
- Correct it by making the counter global, or by passing the count in and returning it
Model answer. The counter has been declared and initialised inside the subroutine, which makes it local. A local variable is created when the subroutine starts and destroyed when it ends, so the value reached on one call is gone by the next and the counter is reset to zero every time, which is why it always returns one. There are two reasonable corrections. Declaring the counter as a global variable outside every subroutine means it persists between calls. Alternatively, and more safely, the current count can be passed in as a parameter and the new count returned, which keeps the subroutine self contained.