In simple terms, a function is a collection of code that can be executed several times. Most of us confuse a function with a procedure. But they are separate entities.
|A procedure may or may not be take input.||A function always take input(in FP).|
|A procedure may or may not return output.||A function always returns the output.|
In functional programming, we should use functions as much as possible.
Input: In general programming, we interchangeably use arguments with parameters but they are not the same ones. Arguments are data that we pass in via function call and parameters are data we receive in a function definition.
We can get all arguments without even defining a single parameter, using arguments as array-like object, but it is not a good practice.
Do you know every function has an internal property named arguments. Have you ever notice why it is called arguments not parameters?
Note: An Argument is an array-like object that we can access inside a function. It contains properties that are passed to a function. Since we are talking about the properties that are passed to function not the properties that receive properties as parameters.
- An argument is an array-like object. We can access values with an array index but cannot use array methods like forEach, map, reduce, etc. as it is array-like but not array.
- An argument object contains values of the arguments passed to that function (So it is called arguments). In the below program, we can see the output of arguments. It prints all values that we pass as arguments.
Note: As of ES5, using arguments is not considered a good practice. You should avoid using it. But if you use arguments, the object is limited to length property.
In functional programming, we strictly take care of the number of parameters in a function declaration that is known as arity. We have to take care of arity because sometimes we have to pass a function inside a function then we have to think in terms of compatibility.
Function with arity 1 is known as unary function, Similarly function with arity 2 is known as binary, and with more that 2, the arity is known as n-ary function.
There could be a problem with the length function. Let’s say, if there is a default parameter or rest parameter then that parameter does not include in the length.
Note: It is recommended to return a value from a function in functional programming. In a case you want to return multiple values then you could use an array or an object.
Function can return value(s) As we all know function can return value(s). It can be a primitive, array or it could be an object.
You must be expecting identityObject returns an object but instead, it will return undefined.
A function can return another function
If a function that returns or receives one or more function values, then that function is known as a higher-order function.
In the above program, arr is an array, and we are using the forEach method to print all values. The forEach() is a higher-order function and expects a function as parameters.
We are using higher-order functions in day to day life and FP is no exceptional. FP uses higher-order in every place. Now it is time to introduce closure.
Closure is all about remembering the scope variables even after the scope has changed. We cannot do FP without closure and closure is all about returning a function from another function and remember the scope. We will discuss closure in another article.
ES6 Arrow function: We all have heard about the arrow function. As it has shorter syntax and no function keyword.
Most of the user prefers the arrow function, but I do not recommend using it. Below are the reasons
- Debugging is not easy with the arrow function, because the arrow function is an anonymous function and if in any case we’ll get some bug, and we debug our application then in function stack we won’t get the function name.
- We always have to take care of the syntax of the arrow function described below.
Rules of arrow function:
- We can drop the function keyword.
- We can drop the return keyword if there is a single statement in the arrow function.
- We have to use the return keyword if we want to return an object.