We all know that an object is a collection of name/value pairs that can be a primitive (number, string, boolean…) or another object, for example:
And this is how it sits in memory:
But what makes a function different from a simple object? Well the function is a special object that has references to special things also sitting in memory and that define it as a function, for example:
- Name : The function’s name
- Code: The function’s code that will be executed when it’s called (which is a property of the function not the function itself)
- Arguments: The function’s arguments
- Length: How many arguments the function is expecting
This is our function example sitting in memory:
And just like an object, we can do with functions everything we can do with an object:
- Assigning them to a variable
- Passing them as parameters to other functions
- Returning them from other functions
- Creating them on the fly
And this is the definition of a First Class Function.
Assigning a function to a variable: In this example we started by creating a function using a function expression and assigning it to a variable, this variable can be used later to pass around the function, invoke it, add properties to it…
Now this function we created in line 1 is what we call an anonymous function, it has no name. Well of course we have the variable multiplyByTen to access it, but truly multiplyByTen is not the function itself, it is an object created in the memory that points to a function that has no name, and it looks like this in memory:
Nota Bene: functions created as an expression and assigned to variables are not hoisted. If you want to know more about hoisting, please check my last article.
Expecting a function as a parameter of another function: In line 5, we created a function with the name mapArray that takes two arguments, the first argument is an array of numbers and the second argument is a function. BAM, you’ve got yourself a Higher-order function !
A what? A Higher-order function, another fancy word that describes a simple concept:
When a function accepts another function as an argument, or returns another function as a result, or sometimes does both, it is called a Higher-order function.
In mapArray we loop through the array and for each element inside the array we execute a function that we got as an argument and push the result to a new array that will be then returned. Here it is in memory:
Passing a function as a parameter to another function: In line 15, we invoked the mapArray function with two parameters: an array of number 1 to 5 and the function referenced by the variable multiplyByTen that simply takes a number and multiply it by ten. mapArray’s Execution Context is then created. The code that will take each element in our array, apply the multiplyByTen function to it and store it in a new array is executed and the new array is returned.
With the console.log at the end of our example we get [10, 20, 30, 40, 50] printed to the screen.
> Welcome to functional programming!
This is the basis of functional programming: Functions are first class citizens, We are able to treat functions like any other type, just like a primitive or an object. Pass them around in functions, return them from a function, assign them to a variable and create them on the fly.
Let’s wrap up today’s article with another example, and go through it line by line:
We started in line 1 by creating a function attack(), where:
- We created an anonymous function on the fly (line 2)
- And returned it, making attack() a what? That’s right! A Higher-order function.
Then, we invoked the attack() function in line 7 and assigned its result to the variable throwAttack.
- Now, what exactly does throwAttack variable refer to? That’s right again! A function.
Finally, we invoke the function to which throwAttack refers to, and we get the following printed on the screen: “Son Goku throws a Kamehameha”
Spoiler: Closures, we will meet them next :) Stay tuned!
2- Functions have special properties, like name, arguments, length, code… that define it as a function
3- An anonymous function has no name, and it usually has an object referring to it.
4- When a function is first class, it is treated like any other type, just like primitives or objects: You can assign them to variables, return them from function, pass them as arguments to functions and create them on the fly.
5- Higher-Order Functions are simply functions that take other functions as argument, or return them as a result or both.
6- The basics of functional programming is treating functions as first class citizens.
A note from the Plain English team
We’ve also launched a YouTube and would love for you to support us by subscribing to our Plain English channel
And as always, Plain English wants to help promote good content. If you have an article that you would like to submit to any of our publications, send an email to firstname.lastname@example.org with your Medium username and what you are interested in writing about and we will get back to you!