⬅ Previous Topic
Introduction to Compilers and InterpretersNext Topic ⮕
Variables⬅ Previous Topic
Introduction to Compilers and InterpretersNext Topic ⮕
VariablesA programming paradigm is a style or way of programming. It defines how we structure and organize code to solve problems. The three major paradigms we'll explore are:
Imperative programming focuses on how to achieve a result. You write step-by-step instructions that change the program’s state.
total = 0
for i from 1 to 5:
total = total + i
print total
15
This approach tells the computer exactly what to do — initialize a variable, iterate, and update it. This is explicit and state-based.
Why is it called “imperative”?
Because you give commands (like in imperative sentences in grammar), instructing the computer step-by-step.
Declarative programming focuses on what you want, not how to do it. You describe the result and let the system figure out how to compute it.
sum = sumOfNumbers(1 to 5)
print sum
15
This hides the control flow (looping, iteration), and focuses only on the end result. You declare your intention.
What are the benefits of declarative programming?
It leads to cleaner, more readable code and abstracts low-level implementation details.
Functional programming is a subset of declarative programming that focuses on pure functions and avoiding shared state. It uses concepts like mapping, filtering, and reducing collections of data.
numbers = [1, 2, 3, 4, 5]
sum = reduce(numbers, (acc, curr) => acc + curr)
print sum
15
Here, reduce
is a higher-order function. No variables are modified. Everything is based on pure transformations.
What makes a function “pure”?
A function is pure if:
Paradigm | Focus | State Mutation | Control Flow |
---|---|---|---|
Imperative | How to do it | Yes | Explicit (loops, steps) |
Declarative | What to do | Hidden or minimal | Implicit |
Functional | What to do using functions | No (stateless) | Abstracted via recursion or higher-order functions |
Which paradigm would be best for processing a list of items without modifying global variables?
Answer: Functional programming.
Which paradigm gives you the most control over how the task is performed?
Answer: Imperative programming.
⬅ Previous Topic
Introduction to Compilers and InterpretersNext Topic ⮕
VariablesYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.