What is the difference between declarative and procedural programming paradigms?
Asked Answered
G

8

124

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

What other programming paradigms exist?

Gefell answered 25/10, 2009 at 2:45 Comment(2)
Similar question: #1785164Commission
I’m voting to close this question because it's off-topic to this site.Geithner
C
129

Imperative

There are several sub-paradigms of the imperative programming paradigm, such as the procedural or the object-oriented programming paradigms.

In the imperative programming paradigm, you describe the algorithm step-by-step, at various degrees of abstraction.

Examples of programming languages which support the procedural paradigm:

  • C (and most other legacy languages)
  • PHP, mostly
  • In some sense, all major languages

Object-Oriented

It typically refers to languages that exhibit a hierarchy of types that inherit both methods and state from base types to derived types, but also includes the unusual prototype-based JavaScript.

Examples of programming languages which support the OO paradigm:

  • Java

Declarative

There are several sub-paradigms of the declarative programming paradigm, such as the functional or the logic programming paradigms.

In the declarative programming paradigm, you describe a result or a goal, and you get it via a "black box". The opposite of imperative.

Examples of programming languages which support the declarative programming paradigm:

  • yacc
  • Treetop
  • SQL
  • Regular Expressions
  • lex
  • XSLT
  • markup, troff, CSS, VHDL

Functional

Functional programming emphasizes the application of functions without side effects and without mutable state. The declarative systems above exhibit certain aspects of functional programming.

Examples of programming languages which support the declarative functional paradigm:

  • Haskell
  • OCaml
  • Scheme
  • Erlang
  • F#
  • Scala
Caulicle answered 25/10, 2009 at 2:45 Comment(0)
D
74

Declarative programming is where you say what you want without having to say how to do it. With procedural programming, you have to specify exact steps to get the result.

For example, SQL is more declarative than procedural, because the queries don't specify steps to produce the result.

Dink answered 25/10, 2009 at 2:45 Comment(4)
+1 for good example on SQL. can we have more examples please?Winonawinonah
Mauris: Maybe not a programming language, but HTML is declarative, because you describe what you want (a paragraph with this bit in bold), rather than writing out "draw string, measure string, advance position, etc." Another example is Prolog, where a "program" is a declarative set of facts and relations/deductions, and a query. The Prolog engine figures out how to evaluate the query: you don't need to tell it how to do so. Finally, regular expressions: you describe the pattern rather than spelling out the steps to test for a match.Selfgoverned
MXML (part of the Flex framework) is declarative: You tell it what order you want your objects/containers to be displayed, and it handles the layout depending on whether you've told it to lay itself out horizontally or vertically. ActionScript 3 is procedural with support for OOP paradigms.Switchback
makefiles is another quite famous declarative languageNogging
S
51

Let me give you a real-world example: I need a cup of tea.

Procedural:

  1. Go to kitchen
  2. Get sugar, milk, and tea,
  3. Mix them, and heat over the fire till it boils
  4. Put that in a cup and bring it to me

Declarative:

  1. Get me a cup of tea.

In a procedural language, you define the whole process and provide the steps how to do it. You just provide orders and define how the process will be served.

In a declarative language, you just set the command or order, and let it be on the system how to complete that order. You just need your result without digging into how it should be done.

Stempien answered 25/10, 2009 at 2:45 Comment(4)
This extremely simplified example actually drove the point home for me (a bit). But one thing still bugs me, trying to understand the difference - what if there is no "Get me a cup of tea"-command? Would you then have to create it yourself? If so, wouldn't you create a function similar to the procedural example? I guess what makes me confused is that it seems like procedural and declarative are not fixed properties of any specific language, but rather how you use that language? You could say "Get me a cup of tea" in Java, if that is an available method...Underrate
I wonder if, "1. Tea, Earl Grey, Hot" would be a better example. It's declaring what needs to be delivered, not what actions need to be carried out ('Get me...')Hypothermal
Magnus, your question is absolutely right. In my opinion, the declarative languages are more high level languages and are derived from procedural. Remember the time when developers needs to use command lines to add two numbers? Now you can get SUM of two or more numbers with just a simple function. So in my perspective, if there is no "Get me a cup of tea" command, you need to created it. When the next person will try to use, it will be there. That's according to my poor knowledge. ThanksStempien
Building on JeffUK's comment, both order of events, and imperative verbs (commands) are signs of procedural language. I believe the declarative in this case would simply be "I have a cup of tea".Autogenesis
D
6

Procedural Programming :

In procedural programming, when the program starts, it follows a set of instructions. The instructions may change based on some file or memory content, but overall, it doesn't vary widely. the input to the program is typically not from user input in real-time, but rather from a pre-gathered set of data.

Declarative Programming:

In Declarative Event driven programming centralizes around a body of data with optional actions the program can take on it. For example, each "event" in a word processor is any mouse or keyboard (or file) changes that affect the data, the document(s). They need not be performed in any order. Event driven programming takes the form of small programs (event handlers) that all work on a common set of data, so that each small program can use the same data, the document in this example.

Dx answered 25/10, 2009 at 2:45 Comment(0)
G
1

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.

Goodoh answered 25/10, 2009 at 2:45 Comment(0)
O
0

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.

Openandshut answered 25/10, 2009 at 2:45 Comment(0)
F
-1

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.

Formerly answered 25/10, 2009 at 2:45 Comment(2)
Check this blog linuxacademy.com/blog/linux-academy/ansible-vs-terraform-fightRetina
This is more like a very asserted comment not answerMaiga
W
-2

In procedural approach you encode your instruction to achieve the result. In the declarative approach you define what needs to be solved as the knowledge of solving the problem. Have a look at Procedural or Declarative approach example I implemented in both approaches.

As you would see in the example, in declarative approach, you do not need to instruct HOW to solve the problem.

Wast answered 25/10, 2009 at 2:45 Comment(1)
You're referencing off-site examples as if they were written here. Bring your examples in as part of your answer.Charentemaritime

© 2022 - 2024 — McMap. All rights reserved.