Declarative programming concepts
CIE A-Level Computer Science· 40 min read
1. Core Definition: Declarative vs Imperative★★☆☆☆⏱ 10 min
Declarative Programming
A programming paradigm focused on describing what problem needs to be solved, rather than explicitly coding how to solve it step-by-step.
Example:
An SQL query that returns all students with a grade over 70 is declarative.
Compare declarative and imperative descriptions for the task of sorting a list of numbers
- 1
The imperative approach explicitly defines every step of the solution:
Iterate from index 0 to n-1, compare adjacent elements, swap if out of order, repeat until no swaps. - 2
The declarative approach only describes the desired end state, with no implementation steps:
Return a new list containing all elements of the input, sorted in ascending order.
2. Key Characteristics of Declarative Programming★★☆☆☆⏱ 15 min
Declarative programming has four core characteristics that are regularly asked about in CIE exams:
No mutable side effects: Most declarative languages avoid changing shared state or mutable data, reducing bugs.
High abstraction: Implementation details are hidden from the programmer, who only defines requirements.
Referential transparency: A function will always return the same output for the same input, regardless of where it is called.
No explicit control flow: The language interpreter handles loops, conditionals, and search, not the programmer.
Identify which of the following code snippets is declarative, and explain why:
- 1
Snippet 1:
SELECT name FROM students WHERE grade > 70; - 2
Snippet 2:
result = []; for s in students: if s.grade > 70: result.append(s.name) - 3
Snippet 1 (SQL) is declarative: it only describes what result we want, not how to scan the table or filter rows. This is handled by the SQL engine.
- 4
Snippet 2 is imperative: it explicitly defines the loop, condition, and action to build the result, detailing every step of the process.
Test your understanding of core characteristics
Which of the following is a core feature of declarative programming?
A) Explicit step-by-step control flow
B) Describing what result is required
C) Mutable shared state
D) Low-level memory management
Reveal answer
B —Correct. All other options are core features of imperative programming, not declarative.
3. Sub-paradigms of Declarative Programming★★★☆☆⏱ 15 min
Declarative programming is an umbrella term for two main sub-paradigms that are explicitly covered in the CIE 9618 syllabus:
Sub-paradigm | Core Idea | Example Languages |
|---|---|---|
Logic Programming | Programs are sets of facts and rules; the interpreter solves queries | Prolog |
Functional Programming | Programs are built from pure functions, no mutable state | Haskell, Scala, Python (functional features) |
Explain how Prolog code for ancestor relationships fits the declarative paradigm
- 1
First, we define facts about parent relationships:
- 2
parent(john, mary). parent(mary, ann). - 3
Next, we define rules for ancestor relationships:
- 4
ancestor(X, Y) :- parent(X, Y). ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y). - 5
This code only describes what is true, not how to find the answer to a query like
ancestor(john, ann). The Prolog interpreter handles the search and matching steps, so this fits the declarative 'what not how' rule.
4. Common Pitfalls
Wrong move:
Claiming all declarative languages are functional, and that all functional code is purely declarative.
Why:
Declarative is a broad paradigm category; functional is just one sub-paradigm, and many functional languages allow imperative code.
Correct move:
State that functional programming is a sub-paradigm of declarative programming, and that logic programming is a separate, equally valid sub-paradigm.
Wrong move:
Confusing object-oriented programming (OOP) with declarative programming.
Why:
OOP is an imperative paradigm that organises code around mutable objects and step-by-step methods.
Correct move:
Remember OOP falls under the imperative umbrella, as it focuses on how to change object state to achieve a result.
Wrong move:
Claiming SQL is not declarative because it has procedural extensions.
Why:
CIE classifies core SQL queries as declarative; procedural extensions are optional add-ons not used for standard queries.
Correct move:
Confirm that standard SQL queries are declarative, as they describe the required result not how to retrieve it.
Wrong move:
Thinking declarative code is always more efficient than imperative code.
Why:
Efficiency depends on use case; hiding low-level implementation can make declarative code slower for some tasks.
Correct move:
Recognise that the main benefits of declarative code are reduced complexity and easier debugging, not guaranteed better performance.
5. Quick Reference Cheatsheet
Feature | Declarative Programming | Imperative Programming | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Core Focus | What result is needed | How to produce the result | ||||||||||||||||
Control Flow | Handled by the language | Explicitly defined by programmer | ||||||||||||||||
S | i | d | e | E | f | f | e | c | t | s | ||||||||
U | s | u | a | l | l | y | a | v | o | i | d | e | d | |||||
C | o | m | m | o | n | a | n | d | e | x | p | e | c | t | e | d | ||
Referential Transparency | Typically supported | Not required | Examples SQL, Prolog, Haskell C, Python, Java, OOP languages |
6. Frequently Asked
What is the single most important distinction to remember for the exam?
The core distinction is what vs how: declarative = what result you want, imperative = how to get the result step-by-step.
Is SQL considered a declarative language for CIE 9618?
Yes, standard SQL queries are explicitly classified as declarative in the CIE syllabus, because they describe the result set, not how to retrieve it.
