Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 106 Computing Fundamentals II Chapter 28 “Scope” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106 material developed.

Similar presentations


Presentation on theme: "1 CS 106 Computing Fundamentals II Chapter 28 “Scope” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106 material developed."— Presentation transcript:

1 1 CS 106 Computing Fundamentals II Chapter 28 “Scope” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106 material developed by CS professors: Cynthia Brown & Robert Martin

2 2 Syllabus Scope Scope Example Example Global vs. Local Global vs. Local Advice on Globals Advice on Globals

3 3 Scope of Names In a small program written by one person, it is easy to enforce each variable has a unique nameIn a small program written by one person, it is easy to enforce each variable has a unique name In a large program, one written by many people, it is not so easyIn a large program, 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 conflictsIn fact, we may want to be able to use the same name in different parts of a large program, without conflicts

4 4 Scope Scope is the domain of program text, over which a names is knownScope is the domain of program text, over which a names is known There are two different domains in VBAThere are two different domains in VBA One is named the global scope; that is the range of text of the whole module. This includes any number of subroutines and of functions The other scope is named local; that is just the range of one sub or one function, including formal parameters A global name is visible by default in any SUBA global name is visible by default in any SUB A global name x can be hidden in a SUB if it is re- declared locally by the name xA global name x can be hidden in a SUB if it is re- declared locally by the name x

5 5 Scope and Lifetime of Variables A global variable is visible in al functions and SUBs of a module, unless re-declared locally. A global lives as long as the program is runningA global variable is visible in al functions and SUBs of a module, unless re-declared locally. A global lives as long as the program is running A local variable exists only as long as the SUB or function is runningA local variable exists only as long as the SUB or function is running 5

6 6 Global vs Local: The Code Option Explicit '************************************************************************* ' Illustrate scope '************************************************************************* Dim varA As Double 'create a global variable Sub WorkBook_Open() varA = 0 varA = 0 End Sub Sub ChangeValues() Dim varB As Double ‘varB is local Dim varB As Double ‘varB is local varA = varA + 1 varA = varA + 1 varB = varB + 1 varB = varB + 1 Cells(1, 1).Value = varA Cells(1, 1).Value = varA Cells(1, 2).Value = varB 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 7 What happens… varA exists as long as the workbook is open. Each time we add 1, its value increasesvarA 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 1varB 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 variablesThis illustrates the difference in lifetime between local and global variables

8 8 Advice on Global Variables Use global variables very sparingly. A program with all variables global easily becomes confusing and error-proneUse 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 proceduresUse 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 itIf 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!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 namesGlobal constants are fine; just give them unique names 8


Download ppt "1 CS 106 Computing Fundamentals II Chapter 28 “Scope” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106 material developed."

Similar presentations


Ads by Google