Edexcel International Advanced Level Computer Science, point 5.4.1
Definition
A programming paradigm is a style or way of thinking about writing programs. The procedural paradigm describes a program as a series of instructions that tell the computer exactly what to do, step by step, grouped into procedures and functions (subprograms). It is also called imperative programming.
Function
It lets the programmer break a task into smaller subprograms and control the order in which the steps happen, changing the program's state (its variables) as it goes.
Characteristics
- A program is a sequence of statements carried out in order, using sequence, selection (if) and iteration (loops).
- It is organised into subprograms (procedures and functions) that can be called when needed, with parameters and return values.
- It uses variables whose values change as the program runs (mutable state).
- It follows a top-down design: start with the main task and break it down.
- The programmer says how to solve the problem, not just what the answer should be.
- Examples of languages: Python (used procedurally), C, Pascal, BASIC.
def get_marks():
marks = []
for _ in range(3):
marks.append(int(input("Mark: ")))
return marks
def highest(marks):
best = marks[0]
for m in marks[1:]:
if m > best:
best = m # the state changes step by step
return best
def show(top):
print(f"Highest: {top}")
marks = get_marks() # step 1
top = highest(marks) # step 2
show(top) # step 3
The main program calls each procedure in order, and the data flows from one to the next through parameters and return values.
Operation
- The program starts at the main section and runs statement by statement.
- When a subprogram is called, control jumps to it, runs, and then returns to where it was called from.
- Decisions and loops change the path through the code.
- Variables are updated as the steps happen, and the final state gives the result.
Benefits
- Simple and easy to learn: it matches how people give instructions ("do this, then this").
- Modular: subprograms can be written, tested and reused separately.
- Easy to follow and trace for small and medium programs.
- Efficient: it maps directly onto how the processor works, so it can be fast and use little memory.
- Widely supported, with lots of libraries.
Drawbacks
- Hard to manage in very large programs, as many subprograms share data (especially global variables), and a change in one place can affect others (side effects).
- Data and the code that uses it are kept apart, so it's harder to protect data than in object-oriented programming (13.1.1).
- Not as natural for modelling real-world objects.
- Reuse is limited compared with object-oriented inheritance.
- Debugging can be harder, because state changes over time.
Procedural compared with the declarative paradigm
| Procedural | Declarative (5.4.2) | |
|---|---|---|
| The programmer says | How to do it, step by step | What result is wanted |
| Control flow | Written explicitly by the programmer | Handled by the system |
| State | Variables change | Little or no changing state |
| Examples | Python (procedural style), C | SQL, Prolog, HTML |
Worked example
Task: describe how a procedural program might solve "read a list of prices and output the total with 20% VAT".
- Decompose into subprograms:
get_prices(),total(prices),add_vat(amount)andshow(amount). - The main program calls them in order:
def get_prices():
return [float(x) for x in input("Prices: ").split()]
def total(prices):
return sum(prices)
def add_vat(amount, rate=0.2):
return amount * (1 + rate)
prices = get_prices()
print(f"{add_vat(total(prices)):.2f}")
- Why it's procedural: the program is a series of steps, each done by a subprogram, with data passed along in the order the programmer chose.
- Benefit:
add_vatcan be reused and tested alone. Drawback: if prices were held in a global list changed by several subprograms, it would be hard to track which one altered it.
Exam tip: describe the paradigm by "a sequence of instructions, grouped into procedures, that change the program's state". For benefits and drawbacks, give points that fit the question (for example easy to follow against hard to maintain in large programs), and contrast it with other paradigms when asked.
Check yourself
What is the procedural programming paradigm?
A style in which a program is a sequence of instructions carried out step by step, organised into procedures and functions, that change the program's state.
Give two characteristics of procedural programming
Any two of: statements run in order; uses sequence, selection and iteration; organised into subprograms; uses variables that change; top-down design.
Give one benefit and one drawback of procedural programming
Benefit: simple and easy to follow, with reusable subprograms. Drawback: hard to manage in very large programs where many subprograms share data.
How does procedural programming differ from declarative programming?
Procedural programming says how to solve the problem step by step. Declarative programming says what result is wanted and leaves the system to work out how.