Wize Programming Languages Textbook > Programming Paradigms

Procedural, Functional, Logical and Object-Oriented Paradigms

Programming Paradigms

Every program has to settle the same practical questions: where does the data live, what is allowed to change it, and what decides the order of the work? A paradigm is a consistent set of answers to those questions. Most first courses introduce four: procedural, functional, logic and object-oriented. Recognising them helps you read unfamiliar code, pick language features that fit a problem, and explain why two correct programs for the same task can look nothing alike.

What a paradigm decides

A programming paradigm is a style of organising computation, defined mainly by how a program represents state and how it controls the order of evaluation. A paradigm describes a style, not a language: Python lets you write procedural, functional and object-oriented code in the same file, while Prolog is built around logic.


Two of the four paradigms tell the computer how to proceed, step by step. Procedural code is a sequence of statements grouped into routines; object-oriented code groups statements into methods of objects that own their data. Both are imperative. The other two describe what the answer is. Functional code builds results by composing functions, and logic code states facts and rules that a search engine satisfies. Both are declarative.


The sharpest dividing line is the side effect: any observable change a computation makes beyond returning a value, such as reassigning a variable, modifying a list or printing output. Functional style avoids side effects, so a function behaves like one from mathematics:


f is pure  ⟺  the same input always gives the same output and nothing else changesf \text{ is pure} \iff \text{the same input always gives the same output and nothing else changes}


One task, four styles

Here is one task written in each style: count the days in a temperature log that were above 30 °C.


Procedural. A loop walks the list and updates a counter. The order of the statements is the order of execution, and the variable count is reassigned as the loop runs.


Python
count = 0
for t in temps:
    if t > 30:
        count += 1


Functional. In Haskell, the answer is one expression built from smaller functions. Nothing is reassigned; filter produces a new list and length measures it.


haskell
hotCount temps = length (filter (> 30) temps)


Logic. In Prolog you state what makes a day hot, then ask a question. The query ?- hot(Day). on its own finds which days are hot (here Day = tue) by backtracking through the facts; it lists them rather than counting them. To count, you wrap the query in an aggregate: aggregate_all(count, Goal, N) collects every solution of the goal and binds N to how many there are, here N = 1. You never write the loop.


prolog
temp(mon, 28).
temp(tue, 33).
hot(Day) :- temp(Day, T), T > 30.
?- aggregate_all(count, hot(_), N).


In a Prolog without aggregate_all, the same count comes from findall/3, which gathers the hot days into a list, followed by length/2, which measures that list.


Object-oriented. The data and the operation travel together in an object. Other code asks the object for its answer without touching the list directly.


Python
class WeatherLog:
    def __init__(self, temps):
        self._temps = temps

    def hot_count(self):
        return sum(1 for t in self._temps if t > 30)



Comparing the four paradigms


Feature

Procedural

Functional

Logic

Object-oriented

Basic unit

Procedure or routine

Pure function

Fact and rule

Object and its class

State

Shared variables, reassigned

Immutable values

Bindings found by search

Fields inside each object

Control flow

Statements in written order

Composition and recursion

Search with backtracking

Method calls between objects

Typical languages

C, Pascal

Haskell, Lisp

Prolog, Datalog

Java, C++

Good fit

Step-by-step algorithms, scripts

Data transformation pipelines

Rules, constraints, queries

Large systems of interacting parts


Because functional code does not reassign variables, it replaces loops with recursion or with higher-order functions such as map, filter and reduce. Logic code has no loops or explicit sequencing of steps: the order of results comes from how the engine searches the clauses, top to bottom and left to right.


Object-oriented code adds three organising ideas on top of imperative statements: encapsulation (an object controls access to its own data), inheritance (a class extends another) and polymorphism (different classes respond to the same method call in their own way). Each object keeps its own copy of the state, so two objects of the same class can hold different values.



What to remember

  • A paradigm is a style of organising state and control; many languages support several.
  • Procedural and object-oriented code is imperative: it says how, step by step, using state that changes.
  • Functional and logic code is declarative: expressions built from pure functions, or facts and rules answered by search.
  • Side effects are the quickest test: functional style avoids them, while procedural and object-oriented code usually relies on them.
  • Choose the paradigm whose organising idea matches the problem: steps, transformations, rules or interacting parts.

Example: One computation in four paradigms

A sensor log stores the readings [3, 4, 7, 10, 12]. An analysis needs the sum of the squares of the even readings. Trace the same computation written in four paradigms and compare how each one reaches its answer.


a) Procedural. Trace the variable total through this loop and give its final value. How many times is total assigned?


Python
data = [3, 4, 7, 10, 12]
total = 0
for x in data:
    if x % 2 == 0:
        total = total + x * x


The loop visits the readings in written order and reassigns total only when the reading is even, starting from 0.

  • For 3, the reading is odd, so total stays 0.
  • For 4, the reading is even, so total becomes ________.
  • For 7, the reading is odd, so total is unchanged.
  • For 10, the reading is even, so total becomes ________.
  • For 12, the reading is even, so total becomes 116 + 144 = 260.

The final value is total = 260. The variable is assigned four times: once when it is initialised and three times inside the loop. A single variable whose value changes as statements run in order is the mark of the procedural style.


b) Functional. The same computation is written as a pipeline. Give the final value of total and state how many variables are reassigned.


Python
from functools import reduce

evens = filter(lambda x: x % 2 == 0, data)
squares = map(lambda x: x * x, evens)
total = reduce(lambda acc, s: acc + s, squares, 0)


Each line builds a new value from the previous one, and nothing is modified afterwards. In Python, filter and map return lazy iterators; the values below are what they yield.

  • filter keeps the even readings: ________.
  • map squares each kept reading: ________.
  • reduce folds the squares into one sum, starting from 0: 0 + 16 + 100 + 144 = 260.

So total = 260, and zero variables are reassigned: each name is bound exactly once. The lambdas are pure, since each output depends only on its inputs. The result matches part a, but the work is expressed as composed transformations rather than a changing accumulator.


c) Logic. In Prolog the computation is a set of clauses and a query. Find the value Prolog reports for S.


prolog
sum_even_sq([], 0).
sum_even_sq([X|Xs], S) :-
    X mod 2 =:= 0,
    sum_even_sq(Xs, S0),
    S is S0 + X*X.
sum_even_sq([X|Xs], S) :-
    X mod 2 =:= 1,
    sum_even_sq(Xs, S).

?- sum_even_sq([3, 4, 7, 10, 12], S).


Prolog tries to prove the query. For a non-empty list it tries the even clause first; if the mod test fails it backtracks and tries the odd clause. Because S is S0 + X*X runs after the recursive call returns, the sum is built from the end of the list back to the front.

  • [] matches the first clause, so its sum is 0.
  • [12] is even, so S becomes ________.
  • [10, 12] is even, so S becomes ________.
  • [7, 10, 12] is odd, so S stays 244.
  • [4, 7, 10, 12] is even, so S = 244 + 16 = 260.
  • [3, 4, 7, 10, 12] is odd, so S stays 260.

Prolog answers S = 260. The program never states a loop; it states what the sum of a list is, and the order of evaluation comes from the search. Notice the partial sums differ from part a because the additions happen in reverse order, yet the final value agrees.


d) Object-oriented. Find the values returned by t.value() and u.value() after this code runs.


Python
class EvenSquareTotal:
    def __init__(self):
        self._total = 0

    def add(self, x):
        if x % 2 == 0:
            self._total += x * x

    def value(self):
        return self._total

t = EvenSquareTotal()
u = EvenSquareTotal()
for x in data:
    t.add(x)
u.add(7)
u.add(10)


Each object has its own _total field, created at 0 by __init__, and other code reaches it only through the methods.


Object t receives all five readings. After t.add(4) its field holds ________, and after the remaining readings it holds 16 + 100 + 144 = 260, so t.value() returns 260, the same arithmetic as part a.


Object u receives 7, which is odd and ignored, then 10, which adds 100. So u.value() returns 100.


Updating u never changes t: state belongs to each object. The calculation is still imperative, but the changing state is bundled with the methods that update it instead of sitting in a shared variable.


All four versions agree on 260 for the full log. What differs is where the state lives (a reassigned variable, no mutable state, bindings found by search, or object fields) and what fixes the order of the work.

Match each programming paradigm or term with the description that characterises it. Each description fits exactly one term in the list.

Match each term
A.

Statements run in written order inside routines that read and reassign shared variables

B.

An observable change beyond returning a value, such as reassigning a variable or printing output

C.

Facts and rules are stated, and a query is answered by searching for bindings that satisfy them

D.

Each entity bundles its own data with the methods that act on that data

E.

Results come from composing pure functions, and values are never modified after they are created

Object-oriented paradigm

Logic paradigm

Side effect

Procedural paradigm

Functional paradigm

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A team implements two components of a sensor application in the same programming language. Complete each description using procedural, functional, logical or object-oriented.


The controller organises work as named procedures executed in an explicit sequence: read a sensor, update a shared running total, and print a report. Its organising paradigm is .


The analysis component organises its computation through the composition of pure functions. These functions return results without changing their inputs or producing observable side effects. Its organising paradigm is .

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A university wants a tool that accepts rules such as 'no student sits two exams in the same slot' and 'the chemistry lab is free only on Tuesday', then reports every timetable that satisfies all of them. The developers want to state the rules and let the system find the solutions instead of programming the search themselves. Which paradigm matches this design most directly?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice