Programming Paradigms
What a programming paradigm is, the characteristics of the low-level, imperative (procedural), object-oriented and declarative paradigms, and how to recognise and describe each one.
What a paradigm is
A paradigm is a style of programming. This chapter compares four: low-level, imperative, object-oriented and declarative.
A programming paradigm is a style of programming. It is a way of thinking about a problem and building its solution. Each paradigm has its own ideas.
| Paradigm | Its main idea |
|---|---|
| Low-level programming | use the processor's own instruction set |
| Imperative (procedural) programming | give a sequence of commands that change the program's state (the values its variables hold), grouped into procedures and functions |
| Object-oriented programming | model the problem as objects; each object bundles data with the methods that act on it |
| Declarative programming | state facts and rules (what is true); the language works out how to answer a question |
Below is a short piece of each. Three of them work out a total; the declarative one describes a family.
LOW-LEVEL
LDD 200
ADD 201
STO 202
IMPERATIVE
Total ← Price + Tax
OBJECT-ORIENTED
Total ← MyBasket.GetTotal()
DECLARATIVE
parent(bat, dorj).
grandparent(X, Z)
IF parent(X, Y) AND parent(Y, Z).Imperative (procedural) programming
An imperative program is a list of commands. They run in order, and each one changes the values of variables.
An imperative program is a sequence of commands. The computer carries them out in the order they are written.
Each command changes the program's state: the values of its variables. It does this by assignment, such as Total ← Price + Tax.
The program is built from:
- sequence: one command after another
- selection: choosing a path, such as with
IF - iteration: repeating commands in a loop.
The commands are organised into procedures and functions with parameters. This is the procedural style you have used since AS Level.
Remember:The programmer says exactly HOW to reach the answer, step by step.
Example:In the code below, the FOR loop calls the function Square 3 times. The program outputs 14.
FUNCTION Square(Num : INTEGER) RETURNS INTEGER RETURN Num * Num ENDFUNCTION DECLARE Total : INTEGER DECLARE Count : INTEGER Total ← 0 FOR Count ← 1 TO 3 Total ← Total + Square(Count) NEXT Count OUTPUT Total
Low-level programming
Low-level code uses the processor's own instructions. Each instruction does one small step with a register or a memory address.
Low-level programming uses the instruction set of the processor itself. An instruction has two parts:
- an opcode: what to do, such as
LDD,ADDorSTO - usually an operand: what to do it with, such as an address.
Each instruction does one small step.
The programmer works directly with the processor's registers (small, fast stores inside the processor) and with memory addresses. In 9618 questions:
- the accumulator,
ACC, is the only general-purpose register IXis the index register.
Addressing modes say how each operand finds its value.
Example:One imperative statement, Total ← Price + Tax, becomes three instructions: load, add and store.
Chapter 28, Low-level Programming, teaches this paradigm in full.
LDD price ; ACC ← [price] ADD tax ; ACC ← ACC + [tax] STO total ; [total] ← ACC END price: 40 tax: 6 total: 0
Object-oriented programming
An object-oriented program is built from objects. Each object is made from a class and holds its own values.
Object-oriented programming (OOP) builds a program from objects.
- A class is a template. It lists the attributes (the data) and the methods (procedures and functions) that its objects will have.
- Each object is an instance of a class, with its own values.
Example: Basket is a class. MyBasket is one object made from it.
OOP has three big ideas:
- Encapsulation keeps an object's data and methods together in one unit. The attributes are usually declared
PRIVATE. So other code must use the object's methods, such as a getter or a setter. - Inheritance lets a subclass take the attributes and methods of its parent class.
- Polymorphism lets a subclass redefine a method.
Chapter 27 teaches this paradigm in full.
CLASS Basket PRIVATE Total : INTEGER PUBLIC PROCEDURE NEW() Total ← 0 ENDPROCEDURE PUBLIC PROCEDURE AddItem(Price : INTEGER) Total ← Total + Price ENDPROCEDURE PUBLIC FUNCTION GetTotal() RETURNS INTEGER RETURN Total ENDFUNCTION ENDCLASS MyBasket ← NEW Basket() MyBasket.AddItem(40) MyBasket.AddItem(6) OUTPUT MyBasket.GetTotal() // 46
Declarative programming
A declarative program says WHAT is true, not HOW to find the answer. The language does the search.
A declarative program states what is true, not how to find an answer. It is a set of:
- facts: things that are true, such as
takes(nomin, maths). - rules: what follows from the facts.
You then set a goal, which is a question. The language searches the facts and rules for every answer that makes the goal true.
The two tabs answer one question: which students does Saraa teach?
| Imperative program | Declarative program | |
|---|---|---|
| What you write | each step yourself: arrays, two loops and two comparisons | one rule |
| Who finds the answer | your steps | the language's search does the rest |
Chapter 29, Declarative Programming, teaches this paradigm in full.
takes(nomin, maths).
takes(bat, maths).
takes(tuya, art).
teaches(saraa, maths).
teaches(dorj, art).
student_of(S, T) IF takes(S, C) AND teaches(T, C).
goal: student_of(S, saraa)
S = nomin, batRecognising a paradigm from its code
Each paradigm leaves clues in its code. Find the clues, then name the paradigm.
An exam may show a few lines of code and ask which paradigm each is written in. Look for the clues.
| Clues in the code | Paradigm |
|---|---|
opcodes such as LDD and STO, with labels or addresses | low-level |
assignments, IF statements and loops, in order, often inside procedures | imperative |
CLASS, NEW, PRIVATE, and calls such as MyBasket.AddItem(40) | object-oriented |
| facts that end with a full stop | declarative |
rules: a head, IF, conditions joined by AND, and a full stop, with no THEN or ENDIF | declarative |
A rule's head is the part before IF: the thing the rule proves.
Watch out:Assignments, IF statements and loops also appear inside the methods of a class. So look for a class around them before you decide.
Describing a paradigm in an exam answer
A two-mark "describe" question needs two separate points, in the words the mark scheme uses.
A question such as "Describe what is meant by an imperative programming language" is worth two marks (2021 Paper 3). So make two separate points, in the words the mark scheme uses.
| Paradigm | Point 1 | Point 2 |
|---|---|---|
| Imperative | a sequence of commands, carried out in the order written | each command changes the state: variables, by assignment |
| Declarative | it states what is wanted, not how | it uses facts and rules to satisfy a goal |
Exam:Describe only the paradigm you are asked about.
Watch out:In a matching question, programs driven by events, such as key presses, match none of the four paradigms.
Total ← 0
FOR Count ← 1 TO N
Total ← Total + Count
NEXT Count
OUTPUT TotalSTARTN = 5
| Count | Total | OUTPUT |
|---|---|---|
One row per pass through the loop. Leave a cell blank if it does not change on that pass.
KEY TERMS
CHECK YOURSELF
1.What is a programming paradigm?
2.Which paradigm uses the processor's own instruction set, with addressing modes that say how each operand finds its value?
3.At AS Level you wrote programs as sequences of statements that change variables by assignment, organised into procedures and functions. Which paradigm is that?
4.Which paradigm models a problem as objects, each holding its own data together with the methods that act on it?
5.In which paradigm does the programmer write facts and rules, then set a goal for the language to satisfy?
6.Which of these is used in object-oriented programming but not in imperative (procedural) programming?
7.What is the main difference between imperative and declarative programming?
8.Which problem is matched with the paradigm that suits it best?
9.Which of these lines is written in a declarative language?
10.A question asks you to match descriptions to the four paradigms. Which description matches none of them?