Scoping – Who Can Communicate With Whom
Lab: Building Blocks of Programming
Video Runtime: 31:44
Let’s talk about scoping, i.e. who can communicate with whom in your program. If you are going to write code, then you must understand scope. It defines the rules to define what code can talk with other code. This is a technical discussion which includes how memory is used and allocated.
Your key takeaways are:
- Local scope means the variables are only accessible within the code block
- Global scope means the variables are accessible everywhere
- Unexpected behavior can happen with global variables
- Do not use global variables in your code
- Garbage collection helps prevent memory leaks
Study Notes
Let’s define scoping:
- It’s a set of rules to define what code can talk with other code
- Variables have scope
- Global scope – everyone can read and write to the variable. WordPress uses a lot of global variables. Yes, this means that some piece of code or plugin could overwrite your global variable, causing unexpected results and faults.
Do not use global variables in your code. - Local scope – access is limited to within the code block where the variable is declared. This is the variable scope you should be using when writing code.
- Global scope – everyone can read and write to the variable. WordPress uses a lot of global variables. Yes, this means that some piece of code or plugin could overwrite your global variable, causing unexpected results and faults.
Declare a Variable
Remember that a variable represents an actual value of data. To declare a variable, you first give it a user-defined name and then you assign it a value. The assignment is called initializing the variable. We call it initializing because we’re setting an initial state for the variable.
pageName = 'FAQ'
What happens when you declare a variable?
- you’re giving it a name
- you’re making an assignment, or initializing the variable
In memory, the user-defined variable is stored in memory or symbols table and then points to the memory location of the data it represents.
Variable Scope
Variables have scope, which is assigned to the code block it resides within. Variables in one scope are not accessible to another scope unless they are global and specifically declared as global within the local scope.
For variables specified within a function call, such as displayPage(pageName)
, the data’s memory location is passed to the function (subroutine) and not the variable itself. When given this code:
pageName = 'FAQ' | |
isPageLoaded = displayPage( pageName ) | |
// pageName in the function is not the same | |
// as the pageName on line 2. | |
function displayPage( pageName ) { | |
isPageLoaded = false | |
... | |
return isPageLoaded | |
} |
the variables pageName
and isPageLoaded
on lines 1 and 2 are not the same variables as in the function displayPage
.
Why? They are in different scopes.
When the function is called in the code above, the location of the data represented by pageName
on line 2 is copied and stored in a new memory location for the local scope of the function.
Calling User-defined Functions
In programs, you have the name of the function (or subroutine) and you can pass it a series of arguments.
What are we doing in the global scope of the following code?
- we initialize the
isPageLoaded
variable by - calling the
displayPage
function and - passing
displayPage
the memory location of thepageName
variable - the result of that function is then assigned to the
ispageLoaded
variable.
// global scope | |
pageName = 'FAQ' | |
isPageLoaded = displayPage( pageName ) | |
// local scope | |
function displayPage( pageName ) { | |
} |
In Step 3 above, since we are only passing the memory location, and not the actual variable, the following code would work too. Do you notice the different variable name used in the function’s arguments?
// global scope | |
pageName = 'FAQ' | |
isPageLoaded = displayPage( pageName ) | |
// local scope | |
function displayPage( anotherPageName ) { | |
} |
More on Local vs. Global Scope
Any variable used inside a function is, by default, in the local scope of that function. Neither other functions nor the main program can access these local variables. They are hidden away inside the function. When that function ends, or when you leave the function by getting to the return
statement, the local variable loses scope. At that point, garbage collection frees up that memory location. If that function is called again, new memory will be allocated to hold the value of the local variable.
This means that a global variable can exist in the global scope, and a variable with the same name can be declared within the local scope of a function or subroutine. The global variable and local variable are not the same variable. They do not point to the same memory location.
The function lives in its own little world. It can only “see” the things inside itself, so it is not aware of any global variables. You can, however, make the function aware of the global variables. You do this by explicitly declaring inside that function that the function’s variable is pointing to the global variable.
Other Resources
There’s a time to code and …. yup, that sums it up.
Episodes
Total Lab Runtime: 02:17:55
- 1 Lab Introductionfree 06:21
- 2 User-Friendly Expression of Informationfree 22:42
- 3 Fundamentals of Syntaxfree 08:57
- 4 Decision Branching - if/then/elsefree 10:39
- 5 Decision Branching – switch/casefree 07:47
- 6 Basics of Iterationfree 14:14
- 7 Iteration in Practicefree 12:51
- 8 Order of Executionfree 10:55
- 9 Abstractionfree 11:45
- 10 Scoping – Who Can Communicate With Whomfree 31:44