# Declarative programming concepts

> CIE A-Level Computer Science · 9618 (2022-2024)
> Source: https://www.owlsprep.com/study/cie-9618-u11-declarative-programming-concepts/

This module covers core declarative programming concepts for CIE 9618 Paper 1. You will compare declarative and imperative paradigms, learn key characteristics, and explore common sub-paradigms tested in exams.

**Prerequisites:** [Basic programming fundamentals](https://www.owlsprep.com/study/cie-9618-u10-programming-fundamentals/)

## Learning objectives

- Distinguish between declarative and imperative programming paradigms
- Identify key characteristics of declarative languages
- Explain the two main sub-paradigms of declarative programming
- Recognise common exam questions about declarative concepts

## Core Definition: Declarative vs Imperative

**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.

**Worked example:** 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.`

> **tip**
>
> Exam questions almost always test the what vs how distinction. Keep this front of mind for comparison questions.

## Key Characteristics of Declarative Programming

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.

**Worked example:** 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.

**Check your understanding**

Test your understanding of core characteristics

1. 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

   *Why:* Correct. All other options are core features of imperative programming, not declarative.

## Sub-paradigms of Declarative Programming

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) |

**Worked example:** 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.

**Exam command terms**

- **Distinguish between declarative and imperative** — You need to state at least one clear difference, usually the what vs how distinction, with an example if asked *(Always mention that declarative describes the required result, imperative describes the steps to get the result.)*

## Common pitfalls

- **Wrong:** Claiming all declarative languages are functional, and that all functional code is purely declarative.
  - Why it fails: Declarative is a broad paradigm category; functional is just one sub-paradigm, and many functional languages allow imperative code.
  - Correct: State that functional programming is a sub-paradigm of declarative programming, and that logic programming is a separate, equally valid sub-paradigm.
- **Wrong:** Confusing object-oriented programming (OOP) with declarative programming.
  - Why it fails: OOP is an imperative paradigm that organises code around mutable objects and step-by-step methods.
  - Correct: Remember OOP falls under the imperative umbrella, as it focuses on how to change object state to achieve a result.
- **Wrong:** Claiming SQL is not declarative because it has procedural extensions.
  - Why it fails: CIE classifies core SQL queries as declarative; procedural extensions are optional add-ons not used for standard queries.
  - Correct: Confirm that standard SQL queries are declarative, as they describe the required result not how to retrieve it.
- **Wrong:** Thinking declarative code is always more efficient than imperative code.
  - Why it fails: Efficiency depends on use case; hiding low-level implementation can make declarative code slower for some tasks.
  - Correct: Recognise that the main benefits of declarative code are reduced complexity and easier debugging, not guaranteed better performance.

## 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 |
| Referential Transparency | Typically supported | Not required |

---

From [OwlsPrep](https://www.owlsprep.com) — free study guides for A-Level, IB, AP and IGCSE, written against the official syllabus. Canonical page: https://www.owlsprep.com/study/cie-9618-u11-declarative-programming-concepts/
