OCR H446, section 1.2.4 (b)
What is a procedural language?
In a procedural language, a program is a sequence of instructions that is organised into procedures and functions (subroutines). Each procedure carries out one job, and the program works by calling them in order. It is an imperative paradigm: the program says how to do the task, step by step, changing the values of variables as it goes.
Examples: C, Pascal, Fortran, BASIC, and Python or JavaScript written in a simple style.
Characteristics
- Sequence, selection and iteration are the building blocks (see 2.2.1).
- The program is broken down into subroutines (top-down design, or stepwise refinement): start with the whole problem, and divide it into smaller tasks.
- Procedures carry out actions. Functions also return a value.
- Data is held in variables and data structures that are passed to subroutines as parameters, or shared as global variables.
- Local variables keep a subroutine's data private.
- Programs run from top to bottom, with control passing into a subroutine and back.
- The data and the code that works on it are kept separate.
Example in OCR pseudocode
function average(a, b, c)
return (a + b + c) / 3
endfunction
procedure showGrade(mark)
if mark >= 70 then
print("Distinction")
elseif mark >= 50 then
print("Pass")
else
print("Fail")
endif
endprocedure
m = average(60, 75, 80)
showGrade(m)
The same program in Python:
def average(a, b, c):
return (a + b + c) / 3
def show_grade(mark):
if mark >= 70:
print("Distinction")
elif mark >= 50:
print("Pass")
else:
print("Fail")
m = average(60, 75, 80)
show_grade(m) # prints Distinction (the average is 71.67)
Structured programming
Procedural programming is closely linked to structured programming, which uses only sequence, selection and iteration, with no jumping about (such as goto), so that the logic can be followed easily from top to bottom.
Advantages
- Simple and easy to learn, and close to how a processor works.
- Subroutines allow reuse: write once and call many times, which saves effort and reduces faults.
- Easy to break up a big problem and share the work between programmers.
- Each subroutine can be tested separately.
- Programs can be fast and efficient, with good control over memory.
- Suited to small to medium programs and tasks that are naturally a sequence of steps.
Disadvantages
- Data and code are separate, so a data structure may be changed by any part of the program, which makes bugs harder to trace, especially with global variables.
- In large programs, it becomes harder to manage: many subroutines work on shared data.
- Less natural for modelling real-world things with their own data and behaviour (object-oriented programming is better).
- Reusing and extending code is less easy than with inheritance.
- Changing how the data is stored may mean changing many subroutines.
Procedural compared with object-oriented
| Procedural | Object-oriented | |
|---|---|---|
| Built from | Procedures and functions | Classes and objects |
| Data and code | Separate | Combined in objects |
| Data access | Often open to any subroutine | Hidden and accessed through methods |
| Reuse | Calling subroutines | Subroutines, inheritance and composition |
| Best for | Small to medium, step-by-step tasks | Large programs modelling many interacting things |
Worked example
Task: a program works out the cost of a taxi journey. Describe how it could be structured as a procedural program.
- Top-down design: the problem splits into: get the distance and waiting time, calculate the fare, add any extras, display the result.
- Each becomes a subroutine:
getJourney(),calculateFare(distance, wait)(a function that returns the fare),addExtras(fare)andshowFare(total)(a procedure). - The main program calls them in order, passing data as parameters and storing results in local variables.
- Each subroutine can be tested on its own, and
calculateFarecan be reused for any journey.
Exam tip: describe a procedural program as a sequence of instructions organised into subroutines, with data separate from the code. Give a strength (simple, reusable subroutines) and a weakness (shared data makes large programs hard to manage).
Check yourself
What is the main structure of a procedural program?
A sequence of instructions, organised into procedures and functions (subroutines) that are called in order.
What is the difference between a procedure and a function?
A function returns a value to the code that called it. A procedure carries out a task without returning a value.
Give one disadvantage of procedural programming for a very large program
Many subroutines work on shared data, so a change to the data in one place can affect many others, which makes bugs hard to trace and the program hard to maintain.