Nested Functions, Closures, and the Missing Link

Nested Functions, Closures, and the Missing Link

nests.jpg

Welcome to Maestro.inc blog. Okay, I know you came here cause you needed to understand a particular concept but before we get into that allow me to lighten the mood.

Screenshot_20191107-210226.png

Okay, let's dive right in!

So this is the first of a three-part series which may or may not contain subparts. In this post I will be talking about Nested functions and why you want to use them. In the process of doing this, I will dive into some other concepts that you may not already be familiar with so definitely stick around to the end cause I will be sure to explain those too.

There is one more thing you need to know before you can fully grasp nested functions, it is called "scope". Odds are if you are here then you probably have already heard, read stumbled into or come across the term scope at one point. Scope is basically the visibility of variables and methods in one part of a program(code-base) to another part of that same program. There are two general types of scope, local and global scope. Local scope(the one you should start practicing from now) is sometimes called functional scope because the variable assigned is only visible inside that function, if called outside that particular function it would result in an error.

 function crust(){
   let baron = "Michael Smith"
   console.log(baron)
}

If you were to run this function you would get the output

crust()

//output
'Michael Smith'

However, if you called the variable baron, you would get an error

baron

//output
ReferenceError: baron is not defined

It works like this in other languages as well, Python, C++, Java etc.

Alright, now that is said and done so what exactly are nested functions?

A nested function is a function which is defined within another function( the enclosing function).

That being said, this type of programming is mostly predominant in functional programs. Now do not get confused or anything if you don't know what functional programming is. Simply put

Functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing state and mutable data

Okay, that is a mouthful, but do not worry about that for now, if you want to find out more about functional programming check out my blog post on Programming Paradigms.

Okay back to Nested functions!

So nested functions are basically functions created and executed inside an enclosing function

Python nested funtion would look something like

def enclose():
       def nested():
              print("this is a nested function running)
        return nested()

while Javascript would be

function enclose(){
       function nested(){
           console.log("you are looking at a nested function")
        }
       return nested()
     }

The main purpose for using nested functions is to eliminate as much global variables from a code-base a possible, cause global variables often make for volatile programs

if you have been making use of global variables before now is a good time to stop that.

Okay so let us together put what you have length into practice with a simple example of a nested function with local variables.

    function nest(name, age){
            let baron = `${name} is ${age}` 
             function inner(){
                     console.log(baron)
                    }
            return inner()
      }

Conclusion

Nested functions are your go-to when using classes to fix a problem will be overkill and plain functions will just not do.

P.S If you are wondering what the missing link is, stick around till the end of the series to find out