Difference between Declarative and Procedural Programming?

During one of my interviews, they asked me the difference between Declarative Programming and Procedural Programming types.

What is the difference between them with examples?

PS : Could you tell me besides these two and object oriented programming, what type programming languages there are? Because they may ask the difference between other programming languages too. And there are similar questions like this one but too scattered.


Declarative

Describe a result and get it via a black box. The opposite of imperative. Examples:

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

    Describe the algorithm and process steps, at various degrees of abstraction.

  • C, most legacy languages
  • PHP, mostly
  • in some sense all major languages
  • Object Oriented

  • Tends to be in the procedural category, 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. A separate dimension from the other categories here.
  • Functional

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

  • Scheme
  • Erlang
  • OCaml
  • Haskell
  • Lisp, depending. (Lisp perhaps deserves its own unique category)
  • Clojure, somewhat
  • Ruby, somewhat less
  • F#
  • Scala

  • 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.


    In simple words, i have a real world example in which i need a cup of tea.

    Procedural:

  • Go to kitchen
  • Get Sugar,Milk and Tea,
  • Mix them and heat over fire till it boils
  • Put that in a cup and bring it to me
  • Declarative:

  • Get me a cup of tea.
  • In 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 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.

    Regards,

    链接地址: http://www.djcxy.com/p/22874.html

    上一篇: React.js中声明式和命令式之间的区别?

    下一篇: 声明式和程序式编程之间的区别?