Imperative Programming and Declarative programming

Chenyun Zhang
1 min readOct 7, 2022

Imperative programming is you tell you program imperative instructions on what to do, when to do, and how to do it. Example for imperative programming language include Java, C, Python, and Ruby.

Declarative programming is what you want the program to achieve. It’s up to the programming language’s implementation and the complier to determine how to achieve the result. Example for declarative programming language include SQL, LISP, and many markup languages. Python can be used in both imperative and declarative programming.

See the difference in code with python:

# Calculate the sum of a list# Imperative way:
arr = [1,2,3]
total = 0
for i in arr: total+=i
print("Total is " + total)
# Declarative way:
arr = [1,2,3]
total = sum(arr)
print("Total is " + total)

Imperative programming instructs the program in details to get the total.

Declarative programming doesn’t know what’s under the hood of sum() method, it only knows sum() returns the total of a given list.

--

--