A-Level Computer Science / Unit 11: Structured Programming

11.1.4 Input, Output and Built-in Routines

🔒 Lesson slides are available to signed-in users. Sign in

11.1.4 Input, Output and Built-in Routines

A useful program needs a way to receive data, process it and communicate results. Pseudocode represents these interactions using INPUT and OUTPUT.

Programs can also call ready-made routines instead of recreating every operation. These routines may measure a string, extract characters, change letter case, truncate a real value or generate a pseudo-random number.

By the end of this section, you should be able to:

  • Write pseudocode that receives a value from the keyboard.
  • Produce clear console output containing text, variables and results.
  • Trace how data moves from input through processing to output.
  • Explain the purpose of built-in functions and library routines.
  • Interpret a supplied function signature.
  • Use common string and numeric routines in expressions.
  • Distinguish a returned value from output displayed on the console.
  • Apply supplied routines without assuming undocumented behaviour.
Connection to earlier pages: declarations and assignment were covered in 11.1.2, while arithmetic and Boolean expressions were covered in 11.1.3. This page uses those skills to build complete interactions.

Input, Processing and Output

Many short algorithms can be understood as a flow of data through three stages.

Input → Processing → Output
Stage Purpose Possible pseudocode
Input Receive data that the algorithm needs INPUT Quantity
Processing Calculate, compare, convert or manipulate the data Total ← Quantity * UnitPrice
Output Communicate a result or message OUTPUT "Total: $", Total
Common misconception: input is not the same as declaring a variable. A declaration prepares a named storage location; an input statement places a user-supplied value into it.

Receiving Keyboard Input

The INPUT command waits for a value and stores it in a variable. The variable must already have been declared with an appropriate data type.

Keyboard input: data entered by the user while an algorithm is running.

Pseudocode pattern

INPUT <Identifier>

Example

DECLARE CrateCount : INTEGER

OUTPUT "Enter the number of crates: "
INPUT CrateCount

The prompt is produced using OUTPUT. The INPUT statement then stores the entered value in CrateCount.

Input must have a destination

Statement Valid? Reason
INPUT CrateCount Yes The value has a variable in which to be stored
INPUT 12 No A literal value is not a storage location
INPUT MAX_CRATES No, if it is a constant A constant must not receive a new value
Exam tip: use a clear prompt before requesting input. It should tell the user what value and, where relevant, what unit or format is expected.

Producing Console Output

The OUTPUT command displays information. It can display literal text, stored values, expression results or values returned by functions.

Console output: information displayed by a running program in a text-based interface.

Pseudocode pattern

OUTPUT <Value or values>
Purpose Example
Display a message OUTPUT "Processing complete"
Display one variable OUTPUT CrateCount
Display text and a variable OUTPUT "Crates loaded: ", CrateCount
Display several values OUTPUT "Section ", Zone, ", shelf ", ShelfNumber
Display an expression result OUTPUT "Spaces remaining: ", Capacity - Occupied
Display a returned value OUTPUT "Characters: ", LENGTH(ItemName)
Common mistake: quotation marks identify literal text. Do not place an identifier inside quotation marks when you want its stored value. OUTPUT "CrateCount" displays the word, not the variable’s value.

Building a Complete Input–Output Sequence

A short interaction normally introduces the required variables, displays a prompt, receives data, performs processing and then labels the result clearly.

Original example: equipment labels

CONSTANT LABEL_PRICE = 1.75

DECLARE LabelCount : INTEGER
DECLARE TotalCost : REAL

OUTPUT "Enter the number of labels required: "
INPUT LabelCount

TotalCost ← LabelCount * LABEL_PRICE

OUTPUT "Labels ordered: ", LabelCount
OUTPUT "Total cost: $", TotalCost
Statement group Role
Declarations Prepare storage for the entered and calculated data
Prompt and input Tell the user what to enter and store the response
Assignment Calculate the required result
Output Display labelled information rather than an unexplained number
Label numerical results. An output such as OUTPUT TotalCost may be technically valid, but OUTPUT "Total cost: $", TotalCost communicates its meaning more clearly.

Using Ready-Made Routines

A programming environment provides routines for operations that programmers frequently need. Calling a ready-made routine avoids writing its internal algorithm each time.

Built-in function: a ready-made operation that receives zero or more values and returns a result.
Library routine: a pre-written operation supplied as part of a reusable collection. In a programming language, the library may need to be imported before the routine is available.

A function call is normally used wherever its returned value is required.

NameLength ← LENGTH(DeviceName)
OUTPUT "Final four characters: ", RIGHT(DeviceName, 4)

In the first statement, the returned integer is assigned to a variable. In the second, the returned string is sent directly to output.

Common misconception: returning a value and displaying a value are different actions. A function may return a result without showing anything on the console.

Reading a Function Signature

A supplied signature explains how a function must be called. It identifies the expected arguments and the type of value returned.

Function signature: a description of a function’s name, inputs and returned data type.
RIGHT(Text : STRING, Count : INTEGER) RETURNS STRING
Part Meaning
RIGHT The function name
Text : STRING The first argument must be a string
Count : INTEGER The second argument must be an integer
RETURNS STRING The function call is replaced by a string result

Valid use

Suffix ← RIGHT("SENSORGRID", 4)

The function receives a string and an integer. It returns "GRID".

Calls that do not match the signature

RIGHT(42, 4)
RIGHT("SENSORGRID", "four")
RIGHT("SENSORGRID")

These calls use the wrong argument type or the wrong number of arguments.

When a function is supplied, read its signature before using it. Do not infer parameter order or return type only from the function’s name.

String Functions and Operations

String routines can count, extract or change characters. The following pseudocode functions illustrate the operations students may need to apply.

Routine or operation Purpose Original example Result
LENGTH(Text) Return the number of characters LENGTH("Greenhouse") 10
RIGHT(Text, Count) Return characters from the right-hand end RIGHT("CYBERLAB", 3) "LAB"
MID(Text, Start, Count) Return a specified number of characters from a position MID("ALGORITHM", 3, 4) "GORI"
UCASE(Character) Return the uppercase equivalent of one character UCASE('m') 'M'
LCASE(Character) Return the lowercase equivalent of one character LCASE('Q') 'q'
& Concatenate strings "Lab " & "Access" "Lab Access"

Positions used by MID

Consider the string "ALGORITHM".

Position 1 2 3 4 5 6 7 8 9
Character A L G O R I T H M

MID("ALGORITHM", 3, 4) starts at position 3 and returns four characters: "GORI".

Common mistake: do not apply Python’s zero-based slicing rules to a supplied pseudocode function. Follow the definition provided for that function.

Numeric Routines

Taking the integer part

The INT function returns the integer part of a real value.

WholeUnits ← INT(14.87)

WholeUnits receives 14. The fractional part is removed; the value is not rounded to 15.

Generating a pseudo-random real value

RAND(Limit) returns a real value from zero up to, but not including, the supplied limit.

RandomReading ← RAND(20)

The result may be a value such as 7.43, satisfying:

0 <= RandomReading AND RandomReading < 20

Generating an integer from 1 to 8

RandomNumber ← INT(RAND(8)) + 1
  1. RAND(8) produces a real value from 0 up to, but not including, 8.
  2. INT produces an integer from 0 to 7.
  3. Adding 1 shifts the possible results to 1 through 8.
Common mistake: INT truncates. It does not perform conventional rounding.
For random-number expressions, derive the lowest and highest possible result rather than assuming that both supplied bounds are included.

Programming-Language Extension

Real programming languages may use different function names and indexing rules. These language-specific forms are useful for implementation, but they should not replace the required pseudocode notation in a pseudocode answer.

Python slicing

Text[start:end]

Python indexes begin at zero. The start index is included and the end index is excluded.

Expression Result for Text = "ALGORITHM"
Text[2:6] "GORI"
Text[:4] "ALGO"
Text[-3:] "THM"

Converting input in a programming language

Some languages initially receive console input as text. A conversion routine may therefore be needed before arithmetic can be performed.

QuantityText = input("Enter quantity: ")
Quantity = int(QuantityText)
This is a programming-language extension. In examined pseudocode, use the function definition or conversion routine supplied with the question.

Worked Example: Asset-Label Generator

A laboratory system receives an asset name and a zone letter. It creates a short label using the final four characters of the asset name and an uppercase version of the zone.

Step 1: declarations

DECLARE AssetName : STRING
DECLARE ZoneLetter : CHAR
DECLARE ShortCode : STRING
DECLARE NormalisedZone : CHAR
DECLARE CharacterCount : INTEGER

Step 2: input

OUTPUT "Enter the asset name: "
INPUT AssetName

OUTPUT "Enter the zone letter: "
INPUT ZoneLetter

Step 3: use the routines

ShortCode ← RIGHT(AssetName, 4)
NormalisedZone ← UCASE(ZoneLetter)
CharacterCount ← LENGTH(AssetName)

Step 4: display labelled results

OUTPUT "Generated label: ", NormalisedZone, "-", ShortCode
OUTPUT "Characters in asset name: ", CharacterCount

Trace using sample input

Input or expression Value or result
AssetName "ROBOTARM"
ZoneLetter 'c'
RIGHT("ROBOTARM", 4) "TARM"
UCASE('c') 'C'
LENGTH("ROBOTARM") 8
Final output Generated label: C-TARM
A complete trace should show the arguments supplied to each routine, the value returned and where that value is used next.

Interactive: Input–Process–Output Tracer

Use the existing tracer to follow data from user input through calculation to console output. Change the input values and inspect the program state after each statement.

Ticket total

Program state

Console output --

Step 1 of 5

Prepare the stored values

Follow the movement of data through the algorithm.

For each step, ask whether data is entering the program, being transformed or being communicated to the user.

Interactive: Built-in Routine Explorer

The string and numeric modes reinforce the general idea of supplying arguments and receiving a return value. The slicing and conversion modes are programming-language extensions.

S[2:6]

Result: GORI

The start index is included and the end index is excluded.

The slice and conversion tabs use Python-style behaviour. Do not transfer those forms directly into a pseudocode answer unless the question defines them.

Implementation Note: Readable Code

Comments can explain why a less-obvious operation is being performed. They are intended for human readers and do not form part of the program’s output.

// Produce an integer from 1 to 8
RandomNumber ← INT(RAND(8)) + 1
Use comments to explain purpose or reasoning. Avoid comments that merely repeat an obvious statement word for word.

Common Mistakes and Misconceptions

  • Putting the prompt inside INPUT: use OUTPUT for the prompt and INPUT Identifier to receive the value.
  • Inputting into a literal or constant: input needs a variable destination.
  • Quoting identifiers: OUTPUT "Total" displays text rather than the value of Total.
  • Unlabelled results: displaying a number without indicating what it represents.
  • Confusing return and output: a returned value is available to the calling expression but is not automatically displayed.
  • Using the wrong argument type: passing text where a routine requires an integer.
  • Ignoring parameter order: swapping the start position and character count in a string function.
  • Assuming all positions start at zero: follow the supplied routine definition rather than applying Python rules.
  • Confusing truncation with rounding: INT(9.96) returns 9.
  • Assuming random bounds: check whether each limit is included or excluded.

Practice

Question 1: keyboard input

Write pseudocode that displays a prompt and receives an integer into RobotCount.

Question 2: labelled output

Write one output statement that displays the text "Available spaces: " followed by the value of SpacesAvailable.

Question 3: several output values

Write one statement that displays a zone letter, shelf number and item count with suitable labels.

Question 4: string routines

Evaluate:

  1. LENGTH("Microchip")
  2. RIGHT("AUTOMATION", 4)
  3. MID("SECURITY", 2, 3)
  4. UCASE('r')

Question 5: read the signature

TAIL(Text : STRING, Count : INTEGER) RETURNS STRING
  1. State the type required for each argument.
  2. State the return type.
  3. Explain why TAIL(27, "four") is invalid.

Question 6: numeric routines

  1. State the result of INT(18.99).
  2. State the smallest possible value of INT(RAND(5)).
  3. State the largest possible value of INT(RAND(5)).
  4. Write an expression that generates an integer from 1 to 5.

Question 7: complete the algorithm

An algorithm receives a workshop name and displays the name together with its length.

DECLARE WorkshopName : STRING
DECLARE NameLength : INTEGER

// Add the missing prompt, input, function call and output.

Question 8: correct the errors

DECLARE Age : INTEGER

INPUT "Enter age: " Age
OUTPUT "Age"
LastPart ← RIGHT(Age, 3)

Identify three problems and suggest corrections.

Show suggested answers

Question 1

OUTPUT "Enter the number of robots: "
INPUT RobotCount

Question 2

OUTPUT "Available spaces: ", SpacesAvailable

Question 3

OUTPUT "Zone: ", ZoneLetter,
       ", shelf: ", ShelfNumber,
       ", items: ", ItemCount

Question 4

  1. 9
  2. "TION"
  3. "ECU"
  4. 'R'

Question 5

  • The first argument must be a STRING.
  • The second argument must be an INTEGER.
  • The function returns a STRING.
  • The supplied call uses an integer for the first argument and a string for the second.

Question 6

  1. 18
  2. 0
  3. 4
  4. INT(RAND(5)) + 1

Question 7

DECLARE WorkshopName : STRING
DECLARE NameLength : INTEGER

OUTPUT "Enter the workshop name: "
INPUT WorkshopName

NameLength ← LENGTH(WorkshopName)

OUTPUT "Workshop: ", WorkshopName
OUTPUT "Number of characters: ", NameLength

Question 8

  • The prompt should be a separate statement: OUTPUT "Enter age: ".
  • Input should then be written as INPUT Age.
  • OUTPUT "Age" displays literal text; use OUTPUT Age or a labelled form.
  • RIGHT requires a string, while Age is an integer.

Review

Concept Key idea Example
Input Receive a value into a variable INPUT Quantity
Output Display one or more values OUTPUT "Quantity: ", Quantity
Built-in function A ready-made operation that returns a value LENGTH(ItemName)
Argument A value supplied to a routine 4 in RIGHT(Name, 4)
Return value The result that replaces the function call A string returned by RIGHT
INT Return the integer part of a real value INT(12.75) = 12
RAND Return a pseudo-random real value below a limit RAND(10)
Final exam tip: for each routine call, identify its arguments, evaluate the returned value, and then state whether that value is assigned, used in another expression or displayed.