How to understand imperative and declarative programming

Ricardo Veronica Duran
4 min readMar 30, 2023

--

Working with different paradigms has the same purpose, solving a problem in the most efficient way, saving unnecessary costs of time, effort and money, so don’t focus on just one programming paradigm, be it procedural, object-oriented or functional, easily you can review all three to include in your toolbox as the professional software developer you want to be.

This is a short introduction to imperative and declarative programming, which tries, with few words and very basic examples, to show the essential difference of both paradigms, for future study and immersion.

Spanish version

Imperative

At the beginning of any learning path in Software development, procedural programming is used, which is nothing more than imperative programming; algorithms which determine the actions that the program has to do to reach the expected result, something like a cooking recipe.

Recipes are like computer programs. There are rules, steps to follow and also guidelines that tell us when the recipe starts and ends to obtain a final result.
Author:
Richard Stallman

That is, in imperative programming, we have to define the flow that the data will follow through structures such as conditionals, loops, and functions.

This paradigm is more focused on Alan Turing’s Turing machine, in which we declare how we want to get to a result. To clarify this a little more, we will see an example.

Example of procedural programming with Python

customers = {'victoria': 100, 'regina': 120, 'alan': 60}

def update_customer_credit(customer_list):
"""
update customer's credit
"""
customer = customers['alan']

if 'alan' in customer_list:
while customer >= 10:
customer -= 10
print(customer)


update_customer_credit(customers)

We declare a key-value dictionary called customers with 3 elements.

The update_customer_credit function asks us for a list of customers.

Within this function by means of a conditional we capture the element that interests us.

By means of a cycle, we subtract the credit that the customer has until reaching zero.

All these steps have the most common structures in programming, variables, functions, cycles and conditionals, with which we can develop imperatively and procedurally, that is, giving orders through the language to reach a result guided by procedures.

Here we can see the expected result.

50
40
30
20
10
0

Declarative

In this paradigm we define the logic, but not the flow control with the structures that we already reviewed, as in the previous example.

The composition of more complex structures such as pure functions or objects is used to start with declarative programming, since in these structures we abstract the imperative components, here only the response is expected and not the way in which it is built.

This type of programming is based on Alonzo Church’s Lambda calculus, which is intended more for the human than for the machine, declaring what the programmer wants to do and not how the machine has to do it.

Lambda Expressions Python Example

Python is a multi-paradigm language with which you can use imperative programming such as object-oriented and also declarative programming such as functional paradigm.

# addition of two numbers
addition = lambda x, y: x + y
print(addition(10, 10)) # 20

# abstraction of two numbers
abstraction = lambda x, y: x - y
print(abstraction(10, 5)) # 5

This code defines a lambda function called “addition” that takes in two arguments, x and y. It then performs the operation of adding x and y together, and returns the result.

Without imperative programming, there can be no declarative programming.

Disclaimer

Take this code for what it is, an example to explain declarative and imperative programming.

In most modern languages there are expressions taken directly from the functional declarative paradigm, for example map, filter, find and reduce, which are the most used functions which we will see in another article.

If you like my content, please consider subscribing and supporting my work by liking, commenting or sharing this or any article I’ve written.

Contact me

ricardo_veronica.duran@hotmail.com

LinkedInGitHub

--

--

Ricardo Veronica Duran

ISC, originario de Guadalajara, enfocado en desarrollo web. Me considero un hombre estoico y un programador pragmático