1

I searched the web to try and understand this term. I understand that 'first class functions' are functions that can be assigned to variables and 'passed around'. But I don't actually understand what this means.

So what does it mean? What are First Class Functions exactly?

Examples would be welcome.

Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131
  • @SteveBenett I looked at what you link here before asking this question, and honestly didn't quite understand. It says that Java only kind-of has first class functions. If so, than what is this in Java: `a = someFunction()`. I don't understand. – Aviv Cohn Mar 28 '14 at 20:34
  • OK, [this](http://stackoverflow.com/questions/1073358/function-pointers-in-java) will explain what a function pointer / first class function in Java is. If you don't know what a function pointer is in general, here is very nice [blog post](http://www.joelonsoftware.com/items/2006/08/01.html) which explains exactly this. This should shed some light on it. – Steve Benett Mar 28 '14 at 20:45

1 Answers1

1

First class functions basically means Functions as a data type just like a string,an array or a number. So in Javascript, functions are data.

You should have a look at :

What is a first class citizen function?

so you can pass functions as arguments of another function:

function map(array,fun){
    var result = [];
    for(var i=0;i<array.length;i++){
       result.push(fun(array[i]));
    }
    return result;
}

map([1,2,3],function(a){return a+1;});   //yields [2,3,4]

Here we have a function that iterate over an array and return the result of an application(passed as a parameter) on each element of the array. So the application fun is a variable therefore data.

If you compare with Java,you cant do that in java (up to 7) without writing classes,therefore functions are not first class in java <=7, unlike integers or floats or classes themself. Java(<=7) only has methods of classes,not functions.

Community
  • 1
  • 1
mpm
  • 20,148
  • 7
  • 50
  • 55