Courses / Computer Science XII (HSSC-II) – FBISE Complete SLO-Based Course

3.1 Programming Paradigms: Procedural, Object-Oriented and Functional

Unit 3 – Programming Fundamentals · 80 min read · 9 MCQs to check yourself

Procedural, object-oriented and functional programming explained

1. What is a programming paradigm?

A programming paradigm is a style or approach to organising and writing programs – a way of thinking about the problem. The purpose of paradigms is to reduce complexity and make code easier to understand, reuse and maintain. Most modern languages (Python, JavaScript, C++) are multi-paradigm.

2. The same problem in three paradigms

Task: find the sum of the squares of the even numbers in a list.

Procedural / imperative – step-by-step instructions that change variables:

numbers = [3, 4, 7, 10, 12]
total = 0
for n in numbers:
    if n % 2 == 0:
        total = total + n * n
print("Procedural:", total)

Output:

Procedural: 260

Object-oriented – bundle the data and the behaviour into an object:

class NumberList:
    def __init__(self, numbers):
        self.numbers = numbers          # state stored in the object
    def even_square_sum(self):
        return sum(n * n for n in self.numbers if n % 2 == 0)

data = NumberList([3, 4, 7, 10, 12])
print("OOP:", data.even_square_sum())

Output:

OOP: 260

Functional – combine pure functions; no variable is changed:

from functools import reduce
numbers = [3, 4, 7, 10, 12]
evens = filter(lambda n: n % 2 == 0, numbers)
squares = map(lambda n: n * n, evens)
print("Functional:", reduce(lambda a, b: a + b, squares))

Output:

Functional: 260

3. Object-oriented programming (OOP) – high-level view

Programs are built from objects that combine data (attributes) and behaviour (methods). Four pillars: encapsulation, abstraction, inheritance, polymorphism (details in the next lesson).

Pros of OOP Cons of OOP
Models real-world things (Student, Account, Car) More code and planning for small programs
Reusable and modular code (classes, inheritance) Can be slower and use more memory
Encapsulation protects data and hides details Deep inheritance hierarchies become complex
Easier for large teams and large systems Shared changing state can cause bugs

Uses: games, mobile apps, GUIs, banking and school management systems, simulations.

4. Functional programming – high-level view

Programs are built from pure functions: the same input always gives the same output, with no side effects (no changing global variables or data). Data is immutable; functions can be passed to other functions (higher-order functions like map, filter, reduce).

Pros of functional programming Cons of functional programming
Fewer bugs – no hidden state changes Hard for beginners (recursion, lambdas)
Easy to test (pure functions) Copying immutable data can use more memory
Safe for parallel / multi-core processing Not natural for programs with lots of state (games, GUIs)
Short, expressive code for data processing Fewer job-ready libraries in pure FP languages

Uses: data analysis pipelines, big-data processing (MapReduce, Spark), financial calculations, compilers.

5. Choosing a paradigm

Application Good fit Reason
Hospital management system OOP Patients, doctors, appointments are natural objects
Analysing millions of sales records Functional Map/filter/reduce run in parallel safely
A short script to rename files Procedural Simple, step-by-step
Querying a database Declarative (SQL) Describe what data you want
A mobile app button Event-driven + OOP Code reacts to taps

Short and long questions

1. What is meant by a programming paradigm? Explain and compare the Functional and Object-Oriented programming paradigms, supporting your answer with suitable examples. (1 + 5)

Model answer

Programming paradigm (1): a style or approach for organising and writing programs; its purpose is to reduce complexity and make code easier to understand, reuse and maintain (e.g. procedural, OOP, functional, declarative).

Comparison (5):

Point Object-oriented Functional
Basic unit Objects created from classes Functions (pure)
Data and behaviour Kept together in an object (attributes + methods) Kept separate; functions transform data
State Objects hold changing (mutable) state Immutable data, no side effects
Key ideas Encapsulation, abstraction, inheritance, polymorphism Pure functions, higher-order functions, recursion, map/filter/reduce
Strengths Models real-world entities; reuse; large team projects Easy testing, fewer bugs, parallel processing
Weaknesses More code; shared state bugs; complex hierarchies Harder to learn; awkward for stateful apps
Typical uses Banking systems, games, GUIs, mobile apps Data pipelines, big data, financial calculations

OOP example:

class Account:
    def __init__(self, balance): self.balance = balance
    def deposit(self, amount): self.balance += amount

Functional example:

total = sum(map(lambda x: x * x, filter(lambda x: x % 2 == 0, [3, 4, 10])))

2. Write Python code to find the sum of squares of even numbers in [3, 4, 7, 10, 12] using (a) procedural and (b) functional style.

Model answer
# (a) procedural
total = 0
for n in [3, 4, 7, 10, 12]:
    if n % 2 == 0:
        total += n * n
print(total)            # 260

# (b) functional
print(sum(map(lambda n: n * n, filter(lambda n: n % 2 == 0, [3, 4, 7, 10, 12]))))   # 260

3. Give the pros and cons of functional programming.

Model answer

Pros: pure functions are easy to test; no hidden state changes → fewer bugs; safe for parallel processing; concise for data processing (map/filter/reduce). Cons: difficult for beginners (recursion, lambdas); immutable copies may use more memory; awkward for programs with much changing state such as games and GUIs.

4. Give three advantages and two disadvantages of object-oriented programming.

Model answer

Advantages: (1) reusable, modular code through classes and inheritance; (2) encapsulation protects data; (3) models real-world entities, so large systems are easier to design and maintain by teams. Disadvantages: (1) more code and planning for small programs; (2) can use more memory/run slower, and deep inheritance becomes complex.

5. What is meant by a programming paradigm? Name any four paradigms.

Model answer

A programming paradigm is a style or approach to structuring and writing programs; it reduces complexity and makes code easier to understand and maintain. Paradigms: procedural (imperative), object-oriented, functional, declarative (e.g. SQL), event-driven, logic.

This lesson ends with a quiz of 9 MCQs. Open the lesson on PrepOasis to take it and save your progress.

Previous: Unit 2 Review, Situational Quiz and Unit Test · Next: 3.2 Object-Oriented Programming in Python: Classes, Objects and State

All lessons in Computer Science XII (HSSC-II) – FBISE Complete SLO-Based Course