β Must-know
π A programming language is a tool such as Python, C++, or Haskell, whereas a programming paradigm is the philosophy behind how code is organized and problems are solved.
Further detail
Paradigm = programming philosophy
π Programming paradigms are divided into two primary branches according to how they handle state and control flow: imperative and declarative.
Imperative asks how; declarative asks what
Mutable state changes in place
β Must-know
Imperative control flow commonly uses for and while loops, explicit if branching, and jumps, whereas declarative control flow uses recursion, higher-order functions, and pattern matching.
Side effects such as writing to disk or modifying global variables are common and expected in imperative programming but minimized or completely eliminated in declarative programming.
Further detail
π Imperative programs primarily use statements, procedures, and objects, whereas declarative programs primarily use expressions, pure functions, and mathematical rules.
Steps versus results
β Must-know
The imperative C solution for summing odd values in [1, 2, 3, 4, 5] initializes a mutable total to 0, loops through the array, tests each value with a conditional, adds odd values to total in place, and returns the total.
The declarative Haskell solution expresses the same task as sum (filter odd numbers), transforming the input with filter and summing the resulting odd values without mutable loops or variables.
Further detail
Loop and mutate versus filter and sum
π Selecting a paradigm that matches a specific domain can reduce bug density in software systems.
Functional immutability can prevent race conditions in multi-threaded systems by avoiding in-place state changes.
Modern enterprise architectures can combine object-oriented models for domain entities, functional pipelines for ETL data-stream processing, and declarative markup such as HTML or YAML for user interfaces and setup configurations.
Match the paradigm to the domain
β Must-know
The function add(x, y) that returns x + y is referentially transparent because, for inputs 2 and 3, it always returns 5 and has no side effects.
The function add_with_log(x, y), which prints βAddingβ before returning x + y, is not referentially transparent because console output is an I/O side effect.
Further detail
π A database query such as SELECT name FROM students WHERE grade > 15 is declarative because it specifies which data to obtain without describing table scans or memory loops.
π Incrementing counter inside a while loop is imperative because it explicitly changes program state step by step, whereas defining f(x) = x^2 + 1 is declarative because it states a mathematical relationship.
Same input, same result, no effects
Imperative Versus Declarative Paradigms
| Feature | Imperative Paradigm | Declarative Paradigm |
|---|---|---|
| Core philosophy | Tell the computer how to solve a problem step by step | State what result is required |
| State management | Mutable state; variables change values in memory | Immutable state; data cannot be modified in place |
| Control flow | Loops, explicit branching, and jumps | Recursion, higher-order functions, and pattern matching |
| Side effects | Common and expected | Minimized or completely eliminated |
| Primary units | Statements, procedures, and objects | Expressions, pure functions, and mathematical rules |
| Representative languages | C, C++, Java, and Pascal | Haskell, Prolog, SQL, and HTML |
Test your knowledge on Programming Language Paradigms with 11 multiple-choice questions with detailed corrections.
1. Which option best describes what a programming paradigm is?
2. What is a programming paradigm?
Memorize the key concepts of Programming Language Paradigms with 11 interactive flashcards.
What is a programming paradigm?
A fundamental style or philosophy for structuring computer code.
Programming paradigm
A style or approach for organizing code.
What distinguishes a programming language from a programming paradigm?
A language is a tool, while a paradigm is the organizing philosophy.
Import your course and AI generates sheets, quizzes and flashcards in 30 seconds.
Sheet generator