R: Nested Functions

We use nested function to do coding more efficiently as it allows to execute multiple commands at the same time. In complex data science use cases, we may have to work on nested functions.

Nested Function is a function within function.

We use nested function to avoid complexity, rather than assigning values from each step  to a separate variable we can easily nest them into one another.To create a nested function we can replace the variable name with the code on right hand side of assignment operator.

Example

c = 2

f1 = function(a,b)
{
    (a-b)*c
}

f1(1,2)

f2 = function(a,b,c)
{
    c <<- c     
    f1(a,b)
 }
 
 f2(1,2,c=3) 

Output:

[1] -2
[1] -3