Saturday, December 26, 2009

Scope

Understanding Scope


Scope is the capability of two different variables to have the same name and maintain different values and lifetimes. Listing 3.6 shows two functions, EndDay() and Bar().


LISTING 3.6 LIST05.TXT--Two Variables with Different Scope

01 Public Function EndDay() as Integer
02 Dim x as Integer
03 Dim y as Integer
04
05 x = 2
06 y = 7
07 EndDay = x + y
08 End Function
09
10 Public Function Bar() as Integer
11 Dim x as Integer
12 Dim y as Integer
13
14 x = 12
15 y = 34
16 Bar = x * y
17 End Function

Notice that each function declares variables x and y. Also notice that those variables are assigned different values within each function (lines 5-6 and 14-15). This is possible because each set of variables exists only where it's created. In the function EndDay(), the variables x and y are created in lines 2 and 3. When the function ends in line 8, the variables are removed from memory and no longer exist. (This is known as going out of scope.) The same is true of the x and y variables in the Bar() function. Neither variable set can see the other. If you wanted to create variables i and j, the values of which could be seen by both functions, you would create the variables higher in scope in the General Declarations section of a form or module, using the Public or Private keywords when declaring them.


Documenting Subs and Functions

Although the EarlyExit()function is functionally adequate, it's difficult to implement from project to project. If other programmers wanted to use it, they would have to take more than a passing glance to figure out what the function is about and how to put it to good use. Proper documentation addresses this deficiency.

All subs and functions should have a header, a section of commented code that appears at the top of a code block. The header usually gives a synopsis of the procedure: the procedure name, a description of the arguments and return value if any, and some remarks as to what the procedure is about, with any special instructions. Also in the header is a history of when and who created the code. If any changes are made to the code, a description and date of the changes are added to the header. Finally, the header contains the appropriate copyright information.

You should also comment each task within the procedure. This saves time for others who will maintain your code. Commenting your code will save you a lot of effort when it comes time to revisit the code later. Listing 3.7 shows the ExitEarly() function commented in a professional manner (line numbering has been omitted for the sake of clarity).




LISTING 18.7 18LIST06.TXT--A Well-Documented Function

01 Public Function ExitEarly(iLimit As Integer, _
02 iFlag As Integer) As Integer
03 `****************************************
04 `Sub/Function: ExitEarly
05 `
06 `Arguments: iLimit The upper limit of the For..Next Loop
07 ` iFlag An integer indicating early exit from
08 ` the function. 1 = Exit.
09 ` Other values are ignored.
10 `
11 `Return: The value of the For...Next loop counter
12 `
13 `Remarks: This function is used to demonstrate the way
14 ` to use arguments within a function
15 `
16 `Programmer: Bob Reselman
17 `
18 `History: Created 4/20/98
19 `
20 `Copyright 1998, Macmillan Publishing
21 `****************************************
22
23 Dim i% `Counter variable
24 Dim Limit% `Internal variable for the upper limit of the
25 `For...Next loop
26 Dim Flag% `Internal variable for the exit flag
27
28 `Assign the limit argument to a local variable
29 Limit% = iLimit
30
31 `Assign the state argument to local variable
32 Flag% = iFlag
33
34 `Run a For...Next loop to Limit%
35 For i% = 0 To Limit%
36
37 `If the passed in state is one
38 If Flag% = 1 Then
39
40 `Check to see if i% equals half the value of
41 `the Limit variable
42 If i% = Limit% / 2 Then
43
44 `If it does, pass out the value of i%
45 `at that point
46 ExitEarly = i%
47
48 `Terminate the function; there is no reason
49 `to go on
50 Exit Function
51 End If
52 End If
53 Next i%
54
55 `If you made it this far, the state variable does not
56 `equal one, so pass the value of i% out of the function
57 `by assigning the value of i% to the function name.
58 ExitEarly = i%
59
60 End Function

No comments:

Post a Comment