Imperative Programming and Declarative programming

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.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store