Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scope. Scope of Names In a small program written by one person, it is easy to be sure that each variable has a unique name In a large program, or one.

Similar presentations


Presentation on theme: "Scope. Scope of Names In a small program written by one person, it is easy to be sure that each variable has a unique name In a large program, or one."— Presentation transcript:

1 Scope

2 Scope of Names In a small program written by one person, it is easy to be sure that each variable has a unique name In a large program, or one written by many people, it is not so easy In fact, we may want to be able to use the same name in different parts of a large program, without conflicts

3 Scope Recall our family analogy. Normally in a family each person has a different first (given) name (or if not they are distinguished by nicknames) People in different families can have the same name; there are lots of Mary’s and William’s If there is a famous person with a certain name, then most people think of that person when they hear the name (Oprah, for example) But if a person in your family has that name, you think of them first

4 Scope and Lifetime of Variables Variables can be declared within a subprocedure or function, or at the level of the module A variable that is declared at the level of the module (also called a global variable) can be seen by all the subprocedures and functions in the module, and is in existence as long as the program is running. It is like a famous person that everyone knows by name A variable declared within a subprocedure or function (also called a local variable) can only be seen there, and exists only as long as the subprocedure or function is running 4

5 An Example (code follows) To see the difference between local and global variables, try running the ScopeIllustration Excel sheet In this sheet, the value of global variable varA is displayed in Cell(1,1) (A1) and the value of local variable varB is displayed in Cell(1,2) (B1)

6 Global vs Local: The Code Option Explicit '************************************************************************* ' Illustrate scope '************************************************************************* Dim varA As Double 'create a global variable Sub WorkBook_Open() varA = 0 End Sub Sub ChangeValues() Dim varB As Double ‘varB is local varA = varA + 1 varB = varB + 1 Cells(1, 1).Value = varA Cells(1, 2).Value = varB End Sub 6 Each time you click the button, varA and varB are incremented This procedure sets varA to 0 when the workbook opens

7 What happens… varA exists as long as the workbook is open. Each time we add 1, its value increases varB is created anew each time we press the button, when procedure ChangeValues is called. VBA sets a new variable to 0, so each time the value we see, after adding 1, is 1 This illustrates the difference in lifetime between local and global variables

8 Tricky Case for Scope Normally a global variable is visible everywhere in the module, but what if a local and a global variable have the same name? It’s as if you have a sister named Oprah. Everyone else thinks Oprah is Oprah Winfrey, but you think first of your sister Likewise, inside the procedure with the local variable named varA, the code cannot see or access the global variable; it uses that name for its local variable 8

9 Advice on Global Variables Use global variables very sparingly. A program with all variables global easily becomes confusing and error-prone Use them for quantities that have to be used by different procedures If a quantity is only needed within a single procedure, use a local variable for it Give global variables names that are unique; don’t reuse the same name for a local variable! Global constants are fine; just give them unique names 9


Download ppt "Scope. Scope of Names In a small program written by one person, it is easy to be sure that each variable has a unique name In a large program, or one."

Similar presentations


Ads by Google