Week 2: Programming Paradigms
Introduction
A programming paradigm is a general way or style of writing and organizing computer
programs. Each paradigm provides a different approach to problem-solving, influencing
how programmers think and how code is structured.
The main paradigms are:
1. Procedural (Imperative)
2. Object-Oriented (OOP)
3. Functional
4. Logic (Declarative)
5. Scripting
1. Procedural (Imperative) Programming
Procedural programming focuses on how to perform tasks step by step. A program is made
up of procedures or functions that execute instructions in a specific order.
• Key Idea: Tell the computer how to do something.
• Features:
o Uses variables, loops, and conditional statements.
o Program flow is top-down and follows a sequence of commands.
o Data and functions are usually separate.
• Examples: C, FORTRAN, BASIC, Pascal.
• Real-world Analogy: A cooking recipe — follow step-by-step instructions to get a
result.
• Use Case: Best for simple tasks or systems with clear, ordered steps (e.g., embedded
systems, low-level operations).
2. Object-Oriented Programming (OOP)
OOP organizes programs around objects—entities that combine data (attributes) and
functions (methods).
• Key Idea: Model the program after real-world entities and their interactions.
• Core Principles:
o Encapsulation: Group related data and behavior together.
o Inheritance: Create new classes from existing ones.
o Polymorphism: Use the same method name for different types of actions.
o Abstraction: Hide complex details and show only what’s necessary.
• Examples: Java, C++, Python, C#, Ruby.
• Real-world Analogy: A car object — has data (color, model) and actions (drive,
brake).
• Use Case: Large systems like games, banking software, or GUI applications where
different parts interact.
3. Functional Programming
Functional programming treats computation as the evaluation of mathematical functions
without changing data.
• Key Idea: Focus on what to compute, not how to compute it.
• Features:
o Pure functions (no side effects).
o Immutability (data cannot be changed after creation).
o Higher-order functions (functions that take other functions as arguments).
o Recursion instead of loops.
• Examples: Haskell, Lisp, Scala, OCaml.
• Real-world Analogy: A calculator — same input always gives the same output.
• Use Case: Ideal for data analysis, mathematical computation, and parallel processing.
4. Logic (Declarative) Programming
Logic programming is based on facts, rules, and queries. The programmer specifies what is
true, and the computer figures out how to find the answer.
• Key Idea: Declare what you want, not how to get it.
• Features:
o Uses facts (basic truths) and rules (relationships).
o Solves problems through logical inference.
o Useful for symbolic reasoning and AI applications.
• Example Language: Prolog.
• Real-world Analogy: Asking a question like “Who are all the people older than 18?”
— the program finds the answer logically.
• Use Case: Expert systems, natural language processing, and theorem proving.
5. Scripting Programming
Scripting languages are typically interpreted (not compiled) and are used to automate
repetitive tasks or connect other programs together.
• Key Idea: Quick development and task automation.
• Features:
o Dynamically typed and easy to write.
o Often used for web development, system administration, or data
processing.
o Code runs line by line via an interpreter.
• Examples: Python, Perl, Ruby, JavaScript, Bash.
• Real-world Analogy: A robot following simple commands to perform routine work.
• Use Case: Automating backups, web scripting, data extraction, and AI experiments.