views:

449

answers:

8

I can enumerate many features of functional programming, but when my friend asked me Could you define functional programming for me? I couldn't.

A: 

It's like drawing a picture by using vectors instead of bitmaps - tell the painter how to change the picture instead of what the picture looks like at each step.

It's application of functions as opposed to changing the state.

moogs
+13  A: 

I would say that the defining point of pure functional programming is that all computation is done in functions with no side effects. That is, functions take inputs and return values, but do not change any hidden state, In this paradigm, functions more closely model their mathematical cousins.

This was nailed down for me when I started playing with Erlang, a language with a write-once stack. However, it should be clarified that there is a difference between a programming paradigm, and a programming language. Languages that are generally referred to as functional provide a number of features that encourage or enforce the functional paradigm (e.g., Erlang with it's write-once stack, higher order functions, closures, etc.). However the functional programming paradigm can be applied in many languages (with varying degrees of pain).

John Stauffer
I will add that you can do this in many common programming languages, not ust ErLang/other functional languages. But in a nutshell this is the definition.
Cervo
+6  A: 

From wikipedia:

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast with the imperative programming style that emphasizes changes in state.

Using a functional approach gives the following benefits:

  • Concurrent programming is much easier in functional languages.
  • Functions in FP can never cause side effects - this makes unit testing much easier.
  • Hot Code Deployment in production environments is much easier.
  • Functional languages can be reasoned about mathematically.
  • Lazy evaluation provides potential for performance optimizations.
  • More expressive - closures, pattern matching, advanced type systems etc. allow programmers to 'say what they mean' more readily.
  • Brevity - for some classes of program a functional solution is significantly more concise.

There is a great article with more detail here.

johnstok
You forgot 2 important benefits deriving from the "no-side effects" core aspect of FP: memoization and parallelization become almost trivial, so FP projects can achieve impressive speeds (if I recall it correctly, Google search was implemented using FP because of these reasons)
Joe Pineda
Trivia - typo: 'Functions in FP can ever cause side effects' should have 'never' in place of 'ever', I believe.
Jonathan Leffler
+3  A: 

I have to add that functional programming tends to also abstract control structures of your program as well as the domain - e.g., you no longer do a 'for loop' on some list of things, but you 'map' it with some function to produce the output.

i think functional programming is a state of mind as well as the definition given above.

Chii
A: 

I think John Stauffer mostly has the definition. I would also add that you need to be able to pass functions around. Essentially you need high order functions, meaning you can pass functions around easily (although passing blocks is good enough).

For example a very popular functional call is map. It is basically equivalent to

list is some list of items
OutList is some empty list
foreach item in list
     OutList.append(function(item))
return OutList

so that code is expressed as map(function, list). The revolutionary concept is that function is a function. Javascript is a great example of a language with high order functions. Basically functions can be treated like a variable and passed into functions or returned from functions. C++ and C have function pointers which can be used similarly. .NET delegates can also be used similarly.

then you can think of all sorts of cool abstractions...

Do you have a function AddItemsInList, MultiplyItemsInList, etc..?
Each function takes (List) and returns a single result

You could create (note, many languages do not allow you to pass + around as a function but it seems the clearest way to express the concept)....

AggregateItemsInList(List, combinefunction, StepFunction)

Increment functions work on indexes...better would be to make them work on list using list operations like next and for incTwo next next if it exists.... function incNormal(x) { return x + 1 }

function incTwo(x) { return x + 2 }

AggregateItemsInList(List, +, incNormal)

Want to do every other item? AggegateItemsInList(List, +, incTwo)

Want to multiply? AggregateItemsInList(List, *, incNormal)

Want to add exam scores together?

function AddScores (studenta, studentb) { return studenta.score + studentb.score }

AggregateItemsInList(ListOfStudents, AddScores, incOne)

High order functions are a very powerful abstraction. Instead of having to write custom methods for numbers, strings, students, etc.. you have one aggregate method that loops through a list of anything and you just have to create the addition operation for each data type.

Cervo
+7  A: 

A lot of the definitions so far have emphasized purity, but there are many languages that are considered functional that are not at all pure (e.g., ML, Scheme). I think the key properties that make a language "functional" are:

  1. Higher-order functions. Functions are a built-in datatype no different from integers and booleans. Anonymous functions are easy to create and idiomatic (e.g., lambdas).
  2. Everything is an expression. In imperative languages, a distinction is made between statements, which mutate state and affect control flow, and expressions, which yield values. In functional languages (even impure functional languages), expression evaluation is the fundamental unit of execution.

Given these two properties, you naturally get the behavior we think of as functional (e.g., expressing computations in terms of folds and maps). Eliminating mutable state is a way to make things even more functional.

Chris Conway
I would argue against 2 because the question is about function programming. And you can do this in Python/Perl. The language is imperative. But with map/filter/lamda/passing functions around you can program in a functional style.
Cervo
if-then-else is a bad example, because most languages have a "conditional expression." Python and Perl most definitely have a distinction between statements and expressions
Chris Conway
+3  A: 

Being able to enumerate the features is more useful than trying to define the term itself, as people will use the term "functional programming" in a variety of contexts with many shades of meaning across a continuum, whereas the individual features have individually crisper definitions that are more universally agreed upon.

Below are the features that come to mind. Most people use the term "functional programming" to refer to some subset of those features (the most common/important ones being "purity" and "higher-order functions").

FP features:

  • Purity (a.k.a. immutability, eschewing side-effects, referential transparency)
  • Higher-order functions (e.g. pass a function as a parameter, return it as a result, define anonymous function on the fly as a lambda expression)
  • Laziness (a.k.a. non-strict evaluation, most useful/usable when coupled with purity)
  • Algebraic data types and pattern matching
  • Closures
  • Currying / partial application
  • Parametric polymorphism (a.k.a. generics)
  • Recursion (more prominent as a result of purity)
  • Programming with expressions rather than statements (again, from purity)
  • ...

The more features from the above list you are using, the more likely someone will label what you are doing "functional programming" (and the first two features--purity and higher-order functions--are probably worth the most extra bonus points towards your "FP score").

Brian
A: 

There are two separate definitions:

  • The older definition (first-class functions) has been given by Chris Conway.

  • The newer definition (avoiding side effects like mutation) has been given by John Stauffer. This is more generally known as purely functional programming.

This is a source of much confusion...

Jon Harrop