Introduction to Programming: Variable Scope

Brief overview and definition of Variable Scope

Variable Scope

Many langauges will include the concept of scope to one degree or another. Scope determines where your variables are "visible" and where they are not. Imagine a box, and all sides of this box are a one-way mirror, where you can look out from inside the box, but no one can look into the box from the outside. Variables that are created inside the box are not visible to anyone outside of the box. Contrastingly, any variable created outside can be seen from those that are inside the box just fine.

let x = 10;
{
	let y = 5;
	print(x);
	print(y);
}
print(y);

In many different languages of programming (but not all), scope can be structured using curly braces ({ }). In the previous example, we declare a variable 'x' and initialize (assign) it with a value of 10. We then define the beginning of a new scope by way of the opening curly bracket ({). This new scope extends from the opening bracket until it's matching closing one (more explanation in a minute). This scope can be referred to as the "inner scope", which is a relational term, where as outside the brackets would be the "outer scope", again used relationally.

Remembering the box with the one-way mirror metaphor, the variables defined in the outer scope are clearly visible to the inner scope. However, the variables within the inner scope are completely hidden from the outer scope, just as items inside the box were not visible to those outside the box. This means that we can print the value of our variable 'x' from the inner scope just fine, as well as the variable 'y' which was declared within the inner scope. Our scope then closes with the closing curly bracket (}), and thus we re-enter the outer scope, where we then try to print() our 'y' variable again. Being that we are in the outer scope now, the 'y' variable is not visible to us anymore, because it was created in the inner scope, so this statement will actually likely generate an error in our program (depending on the programming language we are using).

Scope is something that can be a little tricky to follow the first time it's described, and is a very common stumbling point for many beginners. As mentioned earlier, different languages also handle scope in varying ways and to varying degrees. Some langauges will only have two scopes, the global scope (outer), and the not-global, or local scope (inner). Other languages will allow you to define many different scope levels, such as the following example:

let x = 10;
{
	let y = 5;
	{
		let z = 15;
		print(x);
		print(y);
		print(z);
	}
	print(x);
	print(y);
}
print(x);

Here we can see there are 3 scope levels, with 'x' being declared on the outer-most scope, 'y' on the next inner scope, and 'z' on the inner-most scope. As before, each scope can access it's own variables as well as the variables declared in scopes outside of it. You can see by the print() statements which variables we have access to at each scope level. You also may remember that it was previously mentioned that scope boundaries are defined as starting at the opening curly bracket and running until it's matching closing one. This means that when we opened our first scope, and then opened another one inside that, we would have to first close our inner-most scope before we can close the one outside of it.

Proper indentation becomes very critical in this aspect of programming to help you clearly see the different levels of your code. Many languages completely ignore white space (spaces, tabs, returns, etc.), and you could write all this code on a single line if you wanted.

let x = 10;{let y = 5;{let z = 15;print(x);print(y);print(z);}print(x);print(y);}print(x);

The code will still execute as expected, it just makes it very hard for us humans to read it and understand what is going on. For this reason it can also make it very hard to see bugs, which is the term used in programming to denote a piece of code that is doing something incorrectly, or not as intended. Say perhaps you put the closing curly bracket in the wrong spot.

let x = 10;
{
	let y = 5;
}
print(y);
print(x);

As we have learned earlier, this will generate an error in many languages, due to accessing a variable that is in the wrong scope (the 'y' variable). However it does become a little harder to identify when we don't have good indentation to help us see it, such as with the following alternate representation of the same code:

let x = 10;{let y = 5;}print(y);print(x);

It's always a good idea to practice keeping your code looking clean and organized so that it's easier to read and identify issues.