Scopes
Let's learn about local variables and the scope of a variable.
Spheres of visibility
When we discussed variables, we mentioned that the most common type is the local variable. Now, we’ll explore these in detail.
Because we’ve discussed methods, we can look at another important concept: scopes.
According to Wikipedia, in programming, the scope of a name (such as a variable) is the part of a program where the name is valid: where the name can be used to refer to something else. (We’ve slightly modified this to match our own terminology.)
Remember: Names are known (defined) in a certain scope, and unknown (undefined) outside of this scope.
Every time there’s a method call, and the flow of execution enters the method’s body, it enters a new scope or room. Things local to this method’s scope (inside of the room) are only visible in this scope. Outside of it, they’re unknown.
Undefined local variable or method
This is also a good opportunity to talk about an error ...