Lexical Scope

In this lesson, you will learn about lexical scoping and how it can be used to remove redundancies in your code.

In our program for computing the square root of a number, we are using the variable x over and over again. We are defining it in each function even though it’s the same variable throughout the program.

In a previous lesson, we learned that definitions outside a block are visible to definitions inside a block unless they are being shadowed. The process of setting the scope of a variable is known as Lexical scoping and we can use this convention to remove the redundant x variables in our square root program.

Let’s first take a look at the original program.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy