Functional Programming

What is functional programming?  Everything is a mathematical function. Functional programming languages can have objects, but generally those objects are immutable — either arguments or return values to functions. There are no for/next loops, as those imply state changes. Instead, that type of looping is performed with recursion and by passing functions as arguments.

Functional programming requires that functions are first-class, which means that they are treated like any other values and can be passed as arguments to other functions or be returned as a result of a function. Being first-class also means that it is possible to define and manipulate functions from within other functions. Special attention needs to be given to functions that reference local variables from their scope. If such a function escapes their block after being returned from it, the local variables must be retained in memory, as they might be needed later when the function is called.

The following table shows which languages support functional programming (by supporting first-class functions) and for which the functional style is the dominant one.

Language Closures Functional
C No No
Pascal No No
C++ Yes No
Java Yes No
Modula-3 Yes No
Python Yes No
Ruby Yes No
Javascript Yes Yes
Ocaml Yes Yes
Erlang Yes Yes
Haskell Yes Yes


Functional programming is known to provide better support for structured programming than imperative programming. It is easy, for instance, to abstract out a recurring piece of code by creating a higher-order function, which will make the resulting code more declarative and comprehensible.

Functional programs are often shorter and easier to understand than their imperative counterparts. Since various studies have shown that the average programmer’s productivity in terms of lines of code is more or less the same for any programming language, this translates also to higher productivity.

Look the difference:

Class using C and object-oriented:

public static class SumOfSquaresHelper
   public static int Square(int i)
      return i * i;

   public static int SumOfSquares(int n)
      int sum = 0;
      for (int i = 1; i <= n; i++)
         sum += Square(i);
      return sum;

Does the same thing using Functional programming:

let square x = x * x
let sumOfSquares n = [1..n] |> square |> List.sum

Object-oriented x Functional languages

When you anticipate a different kind of software evolution:

  • Object-oriented languages are good when you have a fixed set of operations on things, and as your code evolves, you primarily add new things. This can be accomplished by adding new classes which implement existing methods, and the existing classes are left alone.
  • Functional languages are good when you have a fixed set of things, and as your code evolves, you primarily add new operations on existing things. This can be accomplished by adding new functions which compute with existing data types, and the existing functions are left alone.

When evolution goes the wrong way, you have problems:

  • Adding a new operation to an object-oriented program may require editing many class definitions to add a new method.
  • Adding a new kind of thing to a functional program may require editing many function definitions to add a new case.

I have been reading about what is functional programming on the last weeks (Just because I was curious) and I found a lot of articles, but I summarized here a little piece of each place which helped me to better understand what really is.

If you are interested about functional programming you can click on this link. I found it very interesting and easy to understand.

 See you next week !


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.