Learning Objectives
By the end of this topic you will be able to:
Define a programming paradigm and explain why different paradigms exist
Describe imperative/procedural programming
Describe declarative programming and give examples
Describe functional programming: pure functions, immutability, higher-order functions, function composition
Procedural
Imperative / Procedural Paradigm
Programs consist of a sequence of statements that modify program state (variables). Control structures (selection, iteration) and procedures/functions are used to structure the code.
Sequence: statements execute one after another in a defined order, changing the state of variables as the program progresses.
Procedures and functions: named blocks of code that can be called from multiple places, reducing code duplication. Parameters pass data in; return values pass results out.
Global and local state: variables store the current state of the program; side effects occur when functions modify variables outside their local scope.
Most languages students encounter (Python, VB.NET, Pascal) support procedural programming as their primary paradigm.
Declarative
Declarative Programming
In declarative programming, the programmer describes what result is wanted rather than specifying the steps to achieve it. The execution details are handled by the language runtime or inference engine.
SQL: SELECT name FROM students WHERE grade = 'A' — specifies what data is needed; the database engine decides how to retrieve it.
Prolog: a logic programming language. The programmer defines facts and rules; Prolog uses backward chaining to infer whether a query is true. E.g.: parent(tom, bob). parent(bob, ann).
Backtracking in Prolog: if a goal cannot be satisfied, Prolog backtracks to try alternative solutions automatically without the programmer coding this logic.
Functional
Functional Programming
Pure functions: always produce the same output for the same input and have no side effects (no modification of external state). Makes code easier to test and reason about.
Immutability: once a value is bound, it cannot be changed. Instead of modifying a list, a new list is produced. Prevents bugs from unexpected state mutation.
Higher-order functions: functions that take other functions as arguments or return functions. Examples: map (apply function to each element), filter (keep elements satisfying a predicate), reduce/fold (combine elements).
Function composition: combining simple functions to build more complex ones. f ∘ g means apply g first, then f to the result. Encourages modular, reusable code.
Common Mistakes
Don’t Lose Marks
!
Confusing declarative with functional — both are non-imperative, but declarative means specifying the desired outcome (SQL, Prolog), while functional means computing via pure function evaluation (Haskell). They are different paradigms.
!
Saying functional means using functions in Python — defining a function in Python is procedural. True functional programming requires pure functions with no side effects and immutable data; Python supports a functional style but is not a purely functional language.
!
Forgetting to mention no side effects when describing pure functions — returning the same output for the same input is only half the definition. No modification of external state is equally important and is regularly tested.