📘 CodingMarble Learn

Programming Paradigms: Imperative, Functional and Object Style

A programming paradigm is a style of thinking about how to build a program. Imperative programs give ordered commands that change variables. Functional programs pass data through functions and change nothing in place. Object-oriented programs are made of objects that keep their own data and send messages. Declarative programs (like SQL) state what is wanted, not how. To choose, weigh the problem, the team and the tools.

🎬 Step-by-step story

  1. Our problem: add the squares of the even numbers in a list. A paradigm is a style of solving it. We will use three styles on the same list.
  2. Imperative style. A pointer walks along the list and a variable called total changes again and again: 4, then 20, then 56. The program is a list of orders.
  3. Functional style. The list goes through three machines: filter keeps even numbers, map squares them, fold adds them. The first list never changes.
  4. Object style. Each number is an object that knows its own actions. Even objects square themselves and send the answer to a Sum object.
  5. Which style to choose? Pick a problem and see which bar is tallest. Maths on lists suits functional, games suit objects, machine steps suit imperative.
  6. Free play: change the list length, run each style, and check that all three give the same answer.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why do we need different styles if the answer is the same?

The answer is the same, but how easy the program is to read, test and change differs. Compare the three runs in the 3D.

What does it mean that a variable "changes"?

The stored value is replaced: total goes 0, 4, 20, 56. This changing data is called state, and the 3D board shows it.

If the list never changes, where does the new data go?

Each machine makes a new list in the next row. The top row stays as it was.

How does an object know what to do?

Its class gives it methods like is_even and square. The object only works on its own data and sends its answer onward.

So which paradigm is best?

None is best everywhere. The bars show that the best choice depends on the problem. Real programs mix styles.

Will all three styles still agree for a longer list?

Yes. Move the slider and press Run for each style: the answers match.

What is a programming paradigm?

A paradigm is a style or way of thinking about writing programs. The same problem can be solved in different styles. The computer does not care; people do, because a good style makes a program easier to read, test and change.

Our example problem: add the squares of the even numbers from 1 to 6. Answer: 2² + 4² + 6² = 4 + 16 + 36 = 56.

Most modern languages (Python, JavaScript, Java) allow more than one paradigm.

Imperative paradigm: commands and changing variables

An imperative program is a list of commands in order. It uses variables whose values change. This changing stored data is called state. The usual tools are sequence, selection (if) and loops. When commands are grouped into named procedures it is called procedural.

total = 0
for n in range(1, 7):
    if n % 2 == 0:
        total = total + n * n
print(total)   # 56

Strength: close to how the computer works and easy to follow step by step. Weakness: many changing variables make big programs hard to check.

Functional paradigm: functions in, functions out

A functional program is built from functions. A pure function gives the same output for the same input and changes nothing outside itself (no side effects). Data is not changed in place; a new value is made instead (immutable data).

evens   = filter(lambda n: n % 2 == 0, range(1, 7))
squares = map(lambda n: n * n, evens)
print(sum(squares))   # 56

Strength: short, easy to test, safe when many things run together. Weakness: can feel unusual at first and is not always best for things that must change, like a game screen.

Object-oriented paradigm: objects that talk

An object-oriented program is made of objects. Each object keeps its own data (attributes) and its own actions (methods). A class is the blueprint; objects are made from it. Objects work together by sending messages (calling methods). Keeping data hidden inside the object is called encapsulation. A new class can reuse an old one (inheritance).

class Num:
    def __init__(self, v): self.v = v
    def is_even(self): return self.v % 2 == 0
    def square(self): return self.v * self.v

class Sum:
    def __init__(self): self.total = 0
    def add(self, x): self.total += x

s = Sum()
for v in range(1, 7):
    n = Num(v)
    if n.is_even(): s.add(n.square())
print(s.total)   # 56

Strength: matches real things (players, bank accounts, buttons). Weakness: for tiny jobs it is more code than needed.

Another paradigm: declarative and logic

A declarative program says what result you want, not how to get it. SQL is an example: SELECT name FROM students WHERE marks > 90 does not say how to search. In logic programming (for example Prolog) you write facts and rules and ask questions; the system finds the answers by itself. Functional programming is also often called declarative.

An event-driven program waits for events (a click, a key) and runs a piece of code for each. Web pages work this way, often together with objects.

Writing and evaluating a program in a paradigm

To write a program in a paradigm, follow its thinking:

  1. Imperative: list the steps, name the variables, decide the loop.
  2. Functional: split the job into small pure functions and join them in a pipeline.
  3. Object: find the "things", give each data and actions, decide who sends messages to whom.

To evaluate (judge) your program, ask: Is the answer correct for several lists? Is it easy to read? Is it easy to test? Can I change it later without breaking things?

Tracing helps: write the variable values after each step. For the imperative code above, total goes 0 → 4 → 20 → 56.

Choosing a paradigm for a problem

No paradigm is best for everything. Weigh these:

Real programs mix styles: objects for the structure, functions for the calculations and commands inside methods.

Key formulas and definitions

Worked examples

1. Trace the imperative program for the list 1..6 and write total after every even number.

Start total = 0. n = 2: total = 0 + 4 = 4. n = 4: total = 4 + 16 = 20. n = 6: total = 20 + 36 = 56. The odd numbers 1, 3, 5 are skipped.

2. Write the functional steps for "double every number, then keep those bigger than 5" for [1, 2, 3, 4].

map doubles: [2, 4, 6, 8]. filter keeps > 5: [6, 8]. Order matters: map first, then filter. The original list [1, 2, 3, 4] is not changed.

3. A game has a Player, an Enemy and a Coin. Which paradigm fits and what could each one hold?

Object-oriented. Player: data = life, place; methods = move, jump. Enemy: data = life, speed; methods = chase, attack. Coin: data = value; method = collect. They send messages such as "attack(player)".

4. Compare the three solutions of the sum-of-squares problem in one line each.

Imperative: a loop changes the variable total. Functional: filter, map and fold make the answer with no variable changed. Objects: each Num object squares itself and sends the value to a Sum object. All three give 56.

Common mistakes

Practice quiz

1. Which paradigm builds a program from commands that change variables?
2. In the functional paradigm, "map" does what?
3. A class is best described as:
4. SQL is mainly which paradigm?
5. Sum of squares of even numbers in 1..6 is:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What are the main programming paradigms?

Imperative (including procedural), functional, object-oriented and declarative or logic. Event-driven is another common style.

What is the difference between imperative and functional programming?

Imperative programs give ordered commands that change variables. Functional programs pass data through pure functions and do not change data in place.

How do I choose a programming paradigm?

Look at the problem, the team size, the need for testing and the tools. Many programs mix paradigms.

Where this is taught

NetherlandsHAVO 5 (eindexamenjaar)Elective theme: Programming paradigms
NetherlandsVWO 6 (eindexamenjaar)Elective theme: Programming paradigms

Learn first

Learn next

Related lessons

All Computer Science lessons