Declarative Programming vs Imperative Programming — Explained as if you are five

Ashan Priyadarshana
2 min readDec 21, 2022

Declarative programming is a way of writing computer programs where you describe what you want the program to do, rather than describing how to do it. This means that you tell the computer what you want it to achieve, and it figures out the steps to take on its own.

Imperative programming is a way of writing computer programs where you tell the computer exactly what steps to take to solve a problem. This means that you write a series of instructions that the computer follows step by step.

Here’s an example to help illustrate the difference:

Imagine you want to bake a cake.

In declarative programming, you might write a program that says something like: “I want to bake a cake with 2 cups of flour, 1 cup of sugar, and 1 cup of milk. Use the oven at 350 degrees for 30 minutes.”

In imperative programming, you might write a program that says something like:

  1. Preheat the oven to 350 degrees.
  2. In a mixing bowl, combine 2 cups of flour, 1 cup of sugar, and 1 cup of milk.
  3. Pour the mixture into a cake pan.
  4. Place the cake pan in the oven.
  5. Bake for 30 minutes.

In the declarative example, you just told the computer what you wanted the end result to be and it figured out the steps to get there. In the imperative example, you told the computer every single step it needed to take to bake the cake.

Declarative programming is often easier to read and understand because you don’t have to worry about all the details of how the program is going to accomplish its task. However, imperative programming can give you more control over exactly what the computer does.

--

--

Ashan Priyadarshana

Associate Technical Lead | BSc. Information Technology | MSc. Artificial Intelligence | Founder Programming.lk | GSoC 2017 |