Programming Paradigms
Imperative vs Declarative vs Functional



What are Programming Paradigms?

A 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

Imperative programming focuses on how to achieve a result. You write step-by-step instructions that change the program’s state.

Example: Sum of first 5 numbers

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.

Question:

Why is it called “imperative”?

Because you give commands (like in imperative sentences in grammar), instructing the computer step-by-step.

Declarative Programming

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.

Example: Sum of first 5 numbers

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.

Question:

What are the benefits of declarative programming?

It leads to cleaner, more readable code and abstracts low-level implementation details.

Functional Programming

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.

Example: Sum of first 5 numbers using reduce

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.

Question:

What makes a function “pure”?

A function is pure if:

Paradigm Comparison Table

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

Summary

Quick Quiz

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.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You 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.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M