-1

What is the definitive answer regarding the differences between imperative languages and functional (a sub-set of declarative) programming languages, and can it be explained with practical examples? What are some of the main or most important imperative languages? How about functional languages?

I did read this answer here but it actually made me even more confused... :-( What is difference between functional and imperative programming languages?

Marcelo
  • 21
  • 1
  • 2
    Possible duplicate of [What is difference between functional and imperative programming languages?](https://stackoverflow.com/questions/17826380/what-is-difference-between-functional-and-imperative-programming-languages) – Bergi Nov 22 '17 at 12:36
  • This really depends on the two languages you are using to compare. Is it Java vs. Haskell? And what do you mean by 'important' in your second to last sentence in the first paragraph? – Frank C. Nov 22 '17 at 12:42
  • What about the linked answer confused you? –  Nov 22 '17 at 20:32
  • Hi Amy, I just felt that Ingo's answer was not coherent. He may indeed have answered but for someone who is completely 'ignorant' about programming like me :-) I felt there was a need for a more thorough response. His examples between imperative and declarative were not related so I felt it was confusing. I appreciated Will's answer since he is making a clear distinction between the 2 languages applying the same example. Now, did my answer to you make any sense? I think it was even more confusing now... <:-O – Marcelo Nov 23 '17 at 17:03
  • Hello Frank, I guess that by important I mean 'more relevant'. For instance, I am completely new to programming but I know it is extremely important and need desperately to learn how to code. Having said that, should I learn Java, which still in use today but some regard it as destined to fade away, or should I put the effort and energy learning Python? That is my dilemma... – Marcelo Nov 23 '17 at 17:15
  • https://medium.com/@danielt1263/imperative-vs-declarative-programming-a74f6cceff0e – Daniel T. Dec 29 '17 at 01:42
  • @Marcelo Did you know that Python is actually older than Java? Maybe you should put your energy into one of the more modern languages. – Daniel T. Dec 29 '17 at 02:18

2 Answers2

2

Let’s use this simple code: a = b + c to examine the difference.

When we write a = b + c in an imperative language, like C, we are assigning the current value of b + c to the variable a and nothing more. We aren’t making any fundamental statement about what a is. Rather, we are simply executing a step in a process.

When we write a = b + c in a declarative language, like Microsoft Excel, we are asserting a relationship between a, b and c such that it is always the case that a is the sum of the other two. It’s not a step in a process, it’s an invariant, a guarantee, a declaration of truth.

Functional languages are declarative as well, but almost by accident. In Haskel for example a = b + c also asserts an invariant relationship, but only because b and c are immutable.

Daniel T.
  • 32,821
  • 6
  • 50
  • 72
  • good example with Excel. indeed constraint programming is very close to the declarative paradigm. Functional programming is not constraint programming though. There's nothing declarative about `a = b + c` in Haskell. It defines an arithmetic computation to be performed *in one direction only*. It's just SSA, a *set once* language. – Will Ness Dec 29 '17 at 03:07
1

There's no major difference.

What I mean is, right now a computer is your slave(*) (and you're a very unfortunate king). It is either a very dumb one, where you give him orders about every minute detail yourself. Like, "wash the first horse in my stable". (Or even "take a bucket of water to the first stall in the stable. Wash the horse there. Clean up the first stall in the stable".) Then, "wash my second horse". (Or even, "bucket.... second stall.... etc."). Etc.

Or it is a slightly more knowledgeable slave, where you give him a slightly more general orders, like "wash all the horses in my stable". But actually, you can say this to your initially dumb slave, who got smarter with the years by writing down recipes for each action he was required to perform, so more general recipes could refer to simpler recipes instead of describing every step anew.

So there really is no difference by now. Presumably you adhere to certain restrictions when talking to the second slave, which make it easier for you to describe your next request to him. You're still doing all the describing.

Both are nothing like a general manager to whom you explain your general feelings about how you'd like stuff be done on your behalf, and they figure out the minute details themselves.

Of course a general manager will need lots of assistants, who will probably make use of lots of dumb slaves. And dumberer yet.

A smart general manager will probably understand more about running your empire than you do yourself. They might even replace you at the helm altogether.

The above contends that functional is no subset of declarative.

edit: the second slave refuses to understand certain things from you. You could tell the first one, for example: "n is 1. take bucket of water to the nth stall. n is now 2. wash horse in the nth stall. n is now 3. clean up nth stall". Which will lead to run-time errors.

The smarter slave just won't understand you there. He knows that the first stable can't suddenly become second; it's the first one, always has been. This helps you to not give dumb orders. That's functional – knowing some stuff just isn't done, refusing to follow dumb orders. Imperative, has no problem doing that, at all. So with imperative programming the onus is on you, and you alone; the discipline imposed on you by the functional paradigm lightens your burden.


(*) Sorry for evoking this disgusting paradigm from humanity's dark history; not so distant in some parts of the world even today.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • Hi Will, I absolutely loved your thorough analogy! Thank you so much for taking the time to respond to my question. (*) Oh yes, and it is closer than one may think... https://www.ft.com/content/7cb56786-cda1-11e7-b781-794ce08b24dc – Marcelo Nov 23 '17 at 16:39
  • I'm not so happy with this answer now, actually. the question is really too broad. I guess I felt like ranting. :) accept, though? – Will Ness Nov 23 '17 at 16:40
  • Yes, I can certainly accept it - nonetheless I just want to see what else is out there, if someone can contribute from a different perspective, or even add to what you have already responded and complement your analogy. I will wait a couple of days before accepting a final answer. But now I am indeed curious, why you're not so satisfied? – Marcelo Nov 23 '17 at 16:52
  • OK, totally see your point. I mean, I took too big a step back here. C can be used as a functional language too, that kind of thing. there _are_ differences between functional and imperative; I was more focused on the promise of declarative I guess, taking it to the extreme. But I kinda fixed it in the last paragraph I guess.... – Will Ness Nov 23 '17 at 17:04
  • BTW you probably don't see it but there's 4 close votes on your question at the moment. all it takes is one more vote, and the question will be closed (either as a duplicate, or as "too broad"). if this happens to you few more times, you will be in danger of a question ban. I don't know the particulars re: when the ban kicks in, but if you'd continue to post downvoted, closed questions, it might happen. there's lots of stuff on meta about asking "good" i.e. focused, specific question about code, with some code included, etc. FYI. :) – Will Ness Nov 23 '17 at 17:14