OCR H446, section 2.1.2 (a)
Thinking ahead means planning before you write any code. The first thing to plan is what goes into the solution and what must come out.
Why identify inputs and outputs first?
- It makes you define the problem exactly: what is the program actually for?
- It shows what data must be collected, stored and validated.
- It tells you what the solution must produce, so you can test it against the expected results.
- It helps you spot missing information early, when it is cheap to fix.
- It lets different people build the parts that fit together, because the interfaces are agreed.
Inputs
An input is data the solution needs from outside itself. It can come from:
- A user: typed text, mouse clicks, touch, voice.
- A sensor: temperature, light, movement.
- A file or database: stored records.
- Another program or a network: data sent through an API.
- Parameters passed to a subroutine.
For each input, decide:
| Question | Example (a login screen) |
|---|---|
| What is it? | Username, password |
| What data type? | Strings |
| What range or format is allowed? | 3 to 20 characters; at least 8 for a password |
| Where does it come from? | Typed by the user |
| How will it be validated? | Length check, presence check |
| What if it is wrong or missing? | Show an error message, ask again |
Outputs
An output is what the solution produces for the user or another system. It can be:
- Information on screen (text, graphics, a table).
- Sound, printed copies or signals to hardware (for example turning on a heater).
- Data written to a file or database.
- A return value from a subroutine.
For each output, decide:
- What exactly must be shown or produced?
- In what format (units, decimal places, layout)?
- Who or what receives it?
- When is it produced?
The shape of a solution
Once you know the inputs and outputs, the processing is the part in between: the steps that turn one into the other.
Worked example
Task: a program works out the cost of a taxi journey, using the distance and the number of passengers.
| Detail | |
|---|---|
| Inputs | distance (a real number of miles, greater than 0); passengers (an integer from 1 to 4) |
| Processing | Cost = £3.50 + £2.10 per mile, with a £1 surcharge for more than 2 passengers |
| Outputs | The total cost, shown with two decimal places and a £ sign |
def taxi_cost(distance, passengers):
cost = 3.50 + 2.10 * distance
if passengers > 2:
cost = cost + 1
return cost
d = float(input("Miles: "))
p = int(input("Passengers (1-4): "))
print(f"Cost: £{taxi_cost(d, p):.2f}")
Test with the known outputs: 10 miles with 2 passengers gives 3.50 + 21.00 = £24.50. With 3 passengers it is £25.50. Because the outputs were worked out in advance, the tests can be written before the code.
Exam tip: when asked for the inputs and outputs of a problem, give specific items, not general words. "The distance and the number of passengers" is a better answer than "some numbers". Say what type and range each one has when the question allows.
Check yourself
Why should you identify the inputs and outputs before writing a program?
It defines what the problem really is, shows what data must be collected and validated, and gives expected results to test against.
Give three possible sources of input
Any three of: a user typing, a sensor, a file, a database, another program or network, parameters passed to a subroutine.
A program calculates the area of a rectangle. State the inputs and the output
Inputs: the length and the width. Output: the area.
What is the processing part of a solution?
The steps that turn the inputs into the outputs.