Overview for Functional Programming

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 to the imperative programming style, which emphasizes changes in state.

In practice, the difference between mathematical functions and notion of a “function” used in imperative programming is that the imperative function can have side effect, it changes the program state. Means the same language expression can have different results over time, but functional programming is not, the data is immutable, unchangeable, the same language expression always lead to same result.

Functional programming promotes higher order function, that it is able to pass function as a parameter to other function.

In JavaScript,

var len = function(list) {
return list.length;
var sum = function(list) {
var total = 0;
for(var i = 0; i < list.length; i++) {
total += list[i];
return total;
var avg = function(list) {
var total = c(list, sum);
return total / c(list, len);
var c = function(list, m) {
return m(list);
var a = [1,2,3,4,5];
c(a, sum); // 15
c(a, avg); // 3

Many languages support Closures, the popular one is JavaScript, F#, Erlang, Scala (JVM Language). Java 7 is adding support declaration of Lambda function. Excitingly, PHP 5.3.0 started to support anonymous function now.

Before Java 7 is introduced, we also can declare anonymous classes (a class without assigning reference). From inside the method of anonymous classes to access the variable outside the scope of the anonymous class, the variable should declared as final. Means, the variable must be immutable, unchangeable, otherwise it caused side effect.

final int a = 0;
new SomeClass() {
void someMethod() {
System.out.println("a: " + a);

But sometimes we want to achieve some simple computation, we may just define an anonymous function passed as an argument to other function.

More Information

PHP 5.3.x


Author: fyhao

Jebsen & Jessen Comms Singapore INTI University College Bsc (Hon) of Computer Science, Coventry University

Leave a Reply

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