Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

lihaoquan
lihaoquan
12,045 Points

Declarative vs Imperative vs Procedural programming.

Can anyone explain what these 3 types of programming do? Why and when to use them?

2 Answers

Hey Li Hao Quan,

From my understanding, declarative and imperative are two contrasting programming paradigms.

Declarative programming (also known as functional programming) languages do not attempt to control the flow of a program; they establish desired results i.e. specifying what they want to happen but not how it should happen. HTML is an example of declarative programming because it does not attempt to control the flow of a program; it simply states what it would like to appear but not how it is done.

Imperative programming languages, on the other hand, do attempt to control the flow of a program; they establish commands that will tell the compiler how they wish the code to run but not explicitly what they want to happen.

Procedural programming is a subset of imperative programming where programs are built off of procedures. Pretty much all languages in use today are procedural languages. Object oriented programming languages, such as Java, JavaScript, PHP, etc. etc., are procedural programming languages. A good list of procedural languages exists here: http://en.wikipedia.org/wiki/Category:Procedural_programming_languages

Some more reading: https://msdn.microsoft.com/en-us/library/bb669144.aspx

I hope this helps =]

lihaoquan
lihaoquan
12,045 Points

Am I right to say that Declarative programming just "DELCARES" the desired result, and then have the imperative programs work it out? E.g HTML is declarative programming, it declares how the end result will look like, then, an IMPERATIVE program e.g ( HTML interpreter ) works out how to run the code in order to give the Declarative HTML its desired appearance. Do they work together like this?

From my understanding, HTML is declarative, but you wouldn't count the interpreter as being declarative or imperative because we don't have any interaction with it. CSS and JavaScript would be imperative because they control the flow of the document. I may be wrong, but this has been my understanding of these two concepts.

Hi There,

Declarative describe a result and get it via a black box. Examples: yacc Treetop SQL Regular Expressions lex XSLT markup, troff, CSS, VHDL

Procedural 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 kind of includes prototype-based languages like JavaScript. Somewhat a separate dimension from the other categories here.

Imperative Programming It is what most professional programmers use in their day-to-day jobs. It's the name given to languages like C, C++, Java, COBOL, etc. In imperative programming, you tell the computer what to do. "Computer, add x and y," or "Computer, slap a dialog box onto the screen." And (usually) the computer goes and does it. This is where most of us spend our lives, in looping structures and if-then-else statements and the like.

http://en.wikipedia.org/wiki/Comparison_of_programming_paradigms

I hope this help :)