I've heard a lot of talk about using functional languages such as Haskell as of late. What are some of the big differences, pros and cons of functional programming vs. object-oriented programming?
The biggest difference is that Imperative programming is about Control flow while Functional programming is about Data flow. Another way to say it is that functional programming only uses expressions while in imperative programming both expressions and statements are used.
For example, in imperative programming variables and loops are common when handling state, while in functional programming the state is handled via parameter passing, which avoids side-effects and assignments.
Imperative pseudo-code for a function for calculate the sum of a list (the sum is kept in a variable):
Functional pseudo-code for the same function (the sum is passed as a parameter):
I recommend the presentation Taming Effects with Functional Programming by Simon Peyton-Jones for a good introduction to functional concepts.
Functional programming is based on a declarative model and has its roots from lambda calculus. It offers a lot of great concepts which can be borrowed from more imperative languages like C++ and C#.
Some examples include referential transparency, lambda functions, first class functions, lazy and eager evaluation, and immutability.
If for nothing else learning functional programming is useful for the concepts that it contains. It will change the way you do programming and think about programming. And I would guess that in the future functional programming will be just as important as object oriented programming has been.
To get started you can chose to use a pure functional language such as Haskell, or you can use a hybrid one like F#.
Most good universities will cover functional programming and if you go to school I'd highly suggest you take that course.
Well object oriented programming is nice because it allows you to model your complex problem into hierarchies so you can simplify the problem. But it becomes very hard when you start to consider multi threaded programming while using mutable objects. In such cases you need to use heavy use of synchronization objects and it's near impossible to perfect a large application.
That's where functional programming comes in. Because of things like immutability functional programming really simplifies multi threaded programs. It makes it almost trivially easy to parallelize something when you know that given input X to a function it will always output Y. Also you know that a variable (or value in functional programming) can't change mid use from another thread.
Functional programming and object-oriented programming are orthogonal to each other. You can have both in the same language. Examples: Scala, F#, OCaml etc.
Maybe you meant functional vs imperative, as Jonas suggested?
There is no real versus. They can be perfectly complementary. There are FP languages, which support OOP. But the communities differ in the way they handle modularity.
Users of FP languages tend to achieve modularity through mathematical laws. And prefer proofs to show compliance with their laws.
In imperative OOP users tend to capture the behaviour of the object in test-cases, which can be rerun if the object has changed and achieve in this way modularity.
It is just a small aspect, but I think it is worth mentioning.
(This answer is adapted from an answer to a closed question at StackOverflow.)
One of the big differences between functional programming and object-oriented programming is that each one is better at a different kind of software evolution:
When evolution goes the wrong way, you have problems:
This problem has been well known for many years; in 1998, Phil Wadler dubbed it the "expression problem". Although some researchers think that the expression problem can be addressed with such language features as mixins, a widely accepted solution has yet to hit the mainstream.
You're handed a job application. You fill in your name, contact information, and work history. When you're finished you no longer have a blank application.
Now imagine instead that before writing you overlay it with a clear sheet of cellophane. You write your name. You add another sheet of cellophane. You write your contact information. More cellophane. You write your work history. When you're finished you still have the blank application untouched. You also have three sheets of cellophane each having captured the effect of a single, discrete change.
The former (OOP) embraces the idea of changing things in place while the latter (FP) shuns it. Both are state management paradigms. Both can, using different strategies, capture the effect of completing a job application. OOP changes the starting instrument directly, while FP overlays what came before to effect the appearance of change.
protected by Telastyn Feb 19 at 18:42
Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?