OCR A-Level Computer Science H446
Processors: Structure, Performance and Types
Input, Output and Storage
Systems Software
Applications Generation
Software Development
Types of Programming Language
Exchanging Data
Networks
Web Technologies
Data Types and Number Representation
Data Structures
Boolean Algebra
Legal, Moral, Cultural and Ethical Issues
Elements of Computational Thinking
Problem Solving and Programming
Algorithms
Problem Solving and Programming
Programming techniques: modularity, functions and procedures, and parameter passing
- 3 things to know
- Component 02
Overview
Recall questions on modularity and its benefits, functions and procedures, what a caller needs to know, reusable subroutines, side effects, passing parameters by value and by reference with traces, returning two results, and parameters versus arguments.
What you need to know
Answer each one before you reveal it.
What does passing a parameter by reference actually do?
What is the difference between a parameter and an argument?
Questions from this subtopic
Each one with the model answer underneath.
- 1
What is modularity, and give three real benefits of it.
Model answer
Modularity means splitting a program into separate self-contained sections — modules or subroutines — each with one clear job and a defined interface. Benefits: a large task can be divided among a team who work on different modules simultaneously; each module can be tested and debugged on its own, so a fault is localised to one small piece instead of hunted for across the whole program; modules can be reused elsewhere in this program and in other projects; and maintenance is easier, because a module's internals can be rewritten without affecting anything else so long as its interface stays the same.
- 2
What is the difference between a function and a procedure?
Model answer
A function returns a value to the point at which it was called, so a call to it forms part of an expression: total = total + triple(7). A procedure carries out a task and returns no value, so a call to it stands alone as a statement: greeting("Hamish"). Both may take parameters and both help modularity. In good practice a function just computes and returns an answer without changing anything else, while a procedure exists for its effect — printing, updating a file, redrawing the screen.
- 3
What must a programmer know about a subroutine in order to call it, and why is that the whole point of modularity?
Model answer
Its name; what parameters it expects — how many, in what order, of what type, and whether each is passed by value or by reference; and what comes back, either a returned value for a function or a described effect for a procedure. That set of facts is its interface. Nothing else is needed: the caller does not need to know how it works inside. That is the point. As long as the interface is unchanged, the inside can be completely rewritten — swapping in a faster sorting method, say — and every piece of code that calls it continues to work untouched.
- 4
What makes a subroutine easy to reuse in another program?
Model answer
It does one clearly defined job rather than several loosely related ones. It receives everything it needs through its parameters instead of reading global variables. It returns its result rather than writing it to a fixed place. It does not depend on anything peculiar to one program — a particular file name, a particular screen layout, a particular data structure defined elsewhere. And its interface is documented, so a new caller knows what to supply and what to expect. A subroutine that reads three globals and prints straight to one part of one screen cannot be lifted anywhere.
- 5
What is a side effect, and why does it make a program harder to debug?
Model answer
A side effect is any change a subroutine makes beyond returning its value: altering a global variable, changing a parameter that was passed by reference, writing to a file, changing the display. It makes debugging harder because the result of a call then depends on more than its arguments, so the same call can behave differently at different times, and because a fault can surface in code far away from the subroutine that actually caused it, separating the symptom from the cause. A function with no side effects can be understood completely from its inputs and its returned value, which is why it is easy to test.
- 6
What does passing a parameter by value actually do?
Model answer
A copy of the argument's value is made and placed into the subroutine's parameter, which is a separate local variable living in the subroutine's own stack frame. The subroutine works entirely on that copy. Anything it does to the parameter — assigning to it, incrementing it, swapping it — changes only the copy, which is destroyed when the subroutine returns. The caller's original variable is therefore unchanged after the call, whatever the subroutine did.
Other ways to work this
The same content, four other ways.
What is inside Levo
Six ways to work the same material.
Every question in Problem Solving and Programming, on a schedule.
Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.
Or £46 a year, about £3.83 a month. Cancel anytime.