A declarative programming paradigm focuses on what the program should accomplish rather than how to accomplish it. The programmer describes the desired results, and the underlying execution engine determines the steps needed to achieve those results.
Key Characteristics
Specification of logic without explicit control flow.
Emphasis on expressions, relations, and constraints.
Programs are often shorter and more readable for certain problem domains.
Side‑effects are minimised; many declarative languages are pure (no mutable state).
Evaluation strategy is handled by the language runtime (e.g., lazy evaluation, backtracking).
Common Declarative Languages and Domains
SQL – Queries describe the data to retrieve, not the algorithm to scan tables.
Prolog – Logic programming where facts and rules define relationships; the interpreter performs backtracking to find solutions.
Haskell, Erlang, F# – Functional languages that treat computation as evaluation of mathematical functions.
HTML / CSS – Markup and style languages that declare structure and appearance.
Declarative vs Imperative (Comparison Table)
Aspect
Declarative
Imperative
Focus
What the result should be
How to achieve the result
Control Flow
Implicit, handled by the language runtime
Explicit (loops, conditionals, assignments)
State Changes
Minimised or absent (pure functions, immutable data)
Frequent (variables are updated)
Typical Use‑Cases
Database queries, constraint solving, data transformation, parallel processing
System programming, algorithms requiring fine‑grained control
Example Syntax
SELECT name FROM students WHERE grade > 80;
for(i=0;i<n;i++) total += a[i];
Illustrative Example – SQL Query
Retrieve the names of all students who scored more than 80 in Mathematics:
\$\text{SELECT name FROM students WHERE subject = 'Mathematics' AND score > 80;}\$
The query states the desired result; the database engine decides how to scan indexes, join tables, etc.
Prolog searches for bindings of X, Y, and Z that satisfy both parent facts.
Advantages of Declarative Programming
Higher level of abstraction – easier to reason about what the program does.
Potential for automatic optimisation by the compiler or interpreter.
Reduced likelihood of bugs related to mutable state.
Often more concise, leading to faster development for suitable problems.
Limitations and When Not to Use
Performance can be unpredictable; the hidden execution strategy may be less efficient for certain tasks.
Not well suited for low‑level hardware interaction or real‑time constraints.
Steeper learning curve for developers accustomed to imperative thinking.
Suggested diagram: Flow of a declarative query – user writes a high‑level specification, the engine parses, optimises, and executes, returning results without explicit loops.
Summary
The declarative paradigm shifts the programmer’s role from directing step‑by‑step procedures to specifying desired outcomes. By abstracting control flow and state management, it enables clearer expression of logic in domains such as databases, logic programming, and functional computation. Understanding its characteristics helps students choose the most appropriate paradigm for a given problem.