# JavaScript: One parameter functions

So, functional programming is extremely interesting.
That is as much a fact as 42.It turns out that to use a lot of the interesting stuff with regards to the composition of functions, one parameter functions are the most conducive.
Let us see how to write one parameter functions for functionalities
that are clearly two parameter functionalities like summing two numbers.
The approach is that for a functionality that takes n parameters typically, create a function that takes the first parameter and returns an inner function that now “knows/sees” the first parameter and takes the second parameter and returns another inner function that now “knows/sees” the first and second parameters ( because the nested inner function can see the outer function variables) and “takes” the third parameter and so on until the innermost nth inner function that “knows” the previous n-1 parameters and “takes” the nth parameter and does the actual computation and returns the computation result to it’s parent. This bubbles up to the next level and so on till the caller gets the result. Illustrated through multiple examples below.

1. sum
> var sum = function (n1 , n2){
… return n1 + n2;
… };
undefined
> sum(10,20)
30
One parameter version
> var sum = function ( n1 ) {
… var sum1 = function ( n2) {
….. return n1 + n2;
….. }
… return sum1;
… }
undefined
> sum(10)(20)
30

2. multiply

> var multiply = function (n1 , n2) {
… return n1 * n2;
… }
undefined
> multiply(10,20)
200
One parameter version
> var multiply = function (n1){
… var multiply = function(n2){
….. return n1 * n2;
….. };
… return multiply;
… };
undefined
> multiply(10)(20);
200

3. Concatenate

> var concatenate = function ( string1 , string2 ) {
… return string1 + string2;
… }
undefined
> concatenate(“This “, “That “);
‘This That ‘
One parameter version
> var concatenate = function (string1){
… var concatenate = function ( string2 ) {
….. return string1 + string2;
….. };
… return concatenate;
… };
undefined
> concatenate(“This”)(“ That “);
‘This That ‘

Comparison function: Same principle and methodology applies as for computation functions.

4. GetLargerValue

> var getLargerValue = function ( n1 , n2) {
… if(n1>n2){
….. return n1;
….. } else {
….. return n2;
….. }
… };
undefined
> getLargerValue(10,20);
20
One parameter version
> var getLargerValue = function(n1) {
… var getLargerValue = function(n2){
….. if(n1>n2){
……. return n1;
……. } else {
……. return n2;
……. }
….. };
… return getLargerValue;
… };
undefined
> getLargerValue(10)(20)
20

Three parameter function being turned into one parameter function

5. Volume

> var volume = function ( length, breadth, height) {
… return length * breadth * height;
… };
undefined
> volume(3,4,5);
60
One parameter version
> var volume = function(length){
….. var volume = function(height){
……. return length * breadth * height;
……. };
….. return volume;
….. };
… return volume;
… }
undefined
> volume(3)(4)(5)
60

One parameter functions come very handy with the array methods like forEach,map etc
First parameter is supplied by the array and some functionality is applied on to this by a second parameter if needed for by the function being employed.

6. sum ( to an array)

Adding a specified number to each member of an array.

> var array = [1,2,3,4];
undefined
> var sum = function(n1){
… var sum = function(n2){
….. return n1 + n2;
….. };
… return sum;
… };
undefined
> increaseByFive = array.map(sum(5));
[ 6, 7, 8, 9 ]

Example: Multiply each number in an array by a specific number.

7. multiply ( to an array)

> var multiply = function (n1){
… var multiply = function (n2) {
….. return n1 * n2;
….. };
… return multiply;
… };
undefined
> array
[ 1, 2, 3, 4 ]
> var doubledArray = array.map(multiply(2));
undefined
> doubledArray;
[ 2, 4, 6, 8 ]

Example: Prefixing an array of names with Mr.

8. prefix ( to an array)

> var concatenate = function (s1) {
… var concatenate = function( s2 ) {
….. return s1 + s2;
….. };
… return concatenate;
… };
undefined
> var array = [“Amitabh Bachchan”,”Aamir Khan”,”Mammootty”,”Leonardo Di Caprio”];
undefined
> var MrArray = array.map(concatenate(“Mr .”));
undefined
> MrArray
[ ‘Mr .Amitabh Bachchan’,
‘Mr .Aamir Khan’,
‘Mr .Mammootty’,
‘Mr .Leonardo Di Caprio’ ]
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.