16: In Or Out Of Scope?

Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Categories:

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++