What is the difference between declarative and procedural programming paradigms?

Viewed 86001

What is the difference between the declarative and procedural programming paradigms? Could you please provide some examples?

What other programming paradigms exist?

8 Answers

The main difference between two programming languages are, In procedural programming, we tell the computer how to solve the problem and in declarative programming, we tell the computer what problem we want solved.

To address the ansible comment and maybe provide an example between the differences of the two. Ansible is procedural where as something like puppet or terraform are declarative. For example, you create an ansible yaml file to deploy 10 ec2 instances like this:
-ec2: count: 10 image: ami-058c6e5b73b074cd2 instance_type: t2.micro

If you were to run that file twice, you would end up with 20 t2.micro ec2 instances. If you wrote the equivalent in a declarative language like terraform and ran it twice you would only have 10 t2.micro instances running no matter how many times you ran it. Declarative languages specify end state. Procedural languages just do what you tell it without regard to current or past state.

So YAML is declarative programming language? Because we define what we want instead of writing actual logic.

I am asking this because if anyone knows Ansible which is configuration management tool, it uses YAML but it still falls in procedural language category.

Related