Scope is another concept that’s amazingly similar to your everyday experience. Let’s say you’re at home and ask your mom, “Where are my slippers?” You mom says, “Upstairs.” That’s scope. And you need to understand it to program. Let me explain.

Scope provides the ability to specify the full context or location of what you’re talking about if it’s not immediately obvious.

C++ and C# provide namespaces that help you separate classes, methods, and variables into groups so they don’t cause conflicts. Each namespace is a scope.

We haven’t talked about classes yet but they allow you to group methods, other classes, and variables into a complete unit that acts together. A class also forms a scope.

We’ve discussed methods. Well, methods also form a scope around the variables inside the method. There’s a difference with method scopes though because a method scope also hides everything inside of it from the outside. Any variable defined inside of a method might as well not exist outside of the method.

Listen to the full episode for more or read the full transcript below.

Transcript

The only reason you know what upstairs means is because you were home at the time. Imagine the same situation but this time you’re with your mom in the lobby of a large building. The same answer, upstairs, now causes confusion. You might just scrunch up one side of your face and ask, “What?” But a compiler will give you an error.

Scope provides the ability to specify the full context or location of what you’re talking about if it’s not immediately obvious.

Once you know the location of your slippers, you can use them anywhere.

Just think about an address. How many Washington Avenues do you think there are in the United States? You need to specify the city and state in order to identify one.

C++ and C# provide namespaces that help you separate classes, methods, and variables into groups so they don’t cause conflicts. Each namespace is a scope.

We haven’t talked about classes yet but they allow you to group methods, other classes, and variables into a complete unit that acts together. A class also forms a scope.

We’ve discussed methods. Well, methods also form a scope around the variables inside the method. There’s a difference with method scopes though because a method scope also hides everything inside of it from the outside. Any variable defined inside of a method might as well not exist outside of the method.

Scopes can be nested within one another too. You can have a method inside of a class, inside of another class, inside of a namespace, inside of another namespace. You can keep going with this as far as you want as long as your current scope can contain another scope type.

What do I mean by a scope type? A namespace is one type of scope and can contain other namespaces, classes, methods, variables, enumerations, etc. A class is another scope type but cannot contain namespaces. And methods form a type of scope and cannot contain namespaces or classes. With practice, you’ll learn what can contain what.

Let’s talk for a bit about how you can use scopes. You already know that programs can have methods to wrap up instructions. And the methods can have variables to hold information. And methods can call other methods. What happens if methodA calls methodB and they both declare a variable called size? This is okay because each size variable is scoped to its method and cannot be accessed outside of the method.

Just try to declare a variable called size and then another variable of any type that’s also called size within the same method and watch the compiler complain.

It is possible to do this. Have multiple variables with the same name in the same method, I mean. I don’t recommend you do this because it can get confusing. They need to be in different scopes or you’ll get compiler errors. In C++ and C#, you use opening and closing curly braces to define method bodies, to define what instructions will be run inside if statements, and what instructions to execute inside loops. Each of these curly braces starts and ends a new scope. You can even put opening and closing curly braces all by themselves to define a new scope that’s nested inside of your current scope.

So if you want to ignore my advice and get the same variable name multiple times in the same method, just make sure to declare them in different scopes.

When your program reaches the end of a scope, any variables declared in that scope are said to go out of scope and they’re destroyed. for simple types like ints or chars, this really only means that the compiler won’t let you access them anymore. We’ll talk about what happens to instances of classes that go out of scope in a future episode.

You might also come across something called the global scope. This is just a scope that includes anything not already in another scope.

What happens if you create a variable in the global scope called size? This is called a global variable and is then available anywhere in your program. If inside of a method you declare another variable with the same name, then your new size variable is said to hide the global variable. Any variables declared in a method are called local variables. Once you have a local variable in your method with the same name as the global variable, then anytime you refer to size, you then refer to your own local size variable instead of the one declared globally. I recommend you avoid this. It’s just too unexpected.

But if you have a large program that declares many global variables, and you’re just writing a small method and need to declare a local variable, how can you be sure that the name you choose doesn’t hide a global variable? Do you have to search through all the source code to make sure the name hasn’t already been used? No. You can avoid this in a couple ways.

The best way to avoid this is to stop using global variables entirely.

But if that’s not possible, then you should name your global variables in a special way so they will be easily identifiable as a global variable and also make sure that you never use this naming pattern anywhere else. Some programmers just put a lowercase g at the beginning of any global variable.