Saturday, December 26, 2009

Named Arguments

Using Named Arguments, Avoid problems with named arguments

Using named arguments enables you to avoid problems that might arise in function use because of the ordering of arguments.


You can use named arguments to make passing arguments to a procedure easier. A named argument is the literal name of an argument in a procedure. For example, if you have a function EndDay() that
takes two arguments, NumOne and NumTwo, of type Integer, you define it as follows:

EndDay(NumOne as Integer, NumTwo as Integer) as Integer

To pass a value to the function by using named arguments, you use the names of the arguments and assign values to them by using the := characters. Thus, to pass actual values into EndDay() by using
named arguments, you do the following:

X = EndDay(NumOne:=3, NumTwo:=4)

Exiting Subs and Functions


Sometimes you need to leave a procedure before it finishes. You do this by using the Exit keyword. Listing 3.5 shows the function ExitEarly(), which takes two arguments: an Integer used to determine the
upper limit of a loop and an Integer that flags the function when a special condition exists that requires the function to be exited early.


LISTING 3.5 18LIST05.TXT--Using the ExitEarly() Function

01 Public Function ExitEarly(iLimit As Integer, _
iFlag As Integer) As Integer
02 Dim i%
03 Dim Limit%
04 Dim Flag%
05
06 `Assign the limit argument to a local variable
07 Limit% = iLimit
08
09 `Assign the state argument to local variable
10 Flag%= iFlag
11
12 `Run a For...Next loop to Limit%
13 For i% = 0 To Limit%
14
15 `If the passed in state is one
16 If Flag% = 1 Then
17
18 `Check to see if i% equals half the value of
19 `the Limit variable
20 If i% = Limit% / 2 Then
21
22 `If it does, pass out the value of i%
23 `at that point
24 ExitEarly = i%
25
26 `Terminate the function; there is no
27 `reason to go on
28 Exit Function
29 End If
30 End If
31 Next i%
32
33 `If you made it this far, the flag variable does not
34 `equal one, so pass the value of i% out of the
35 `function by assigning the value of i% to the
36 `function name.
37 ExitEarly = i%
38
39 End Function

The ExitEarly() function works by taking the iLimit argument, assigning it to a variable local to the function (line 7) and then using that local variable to be the upper limit of a For...Next loop (line 13). The
function also takes the iFlag argument and assigns that variable to one that's also local to the function (line 10). Then the For...Next loop is run. Within the loop, if the value of the local Flag variable is 1 (line
16), an If...Then statement checks the value of the counting variable, i%, to see whether it's equal to half the value of the variable Limit (line 20). If it is, the value is assigned to the function's name (line 24) to
be passed back to the call, and the Exit keyword terminates the execution of the function (line 28). If the value of the local variable Flag is other than 1, the loop continues until it reaches its limit (line 31).
Then the value of i% is assigned to the function's name, and control is returned to the calling code (line 36).

When you create a user-defined function, the Visual Basic IDE treats it as though it were an intrinsic function. This means that the function is listed in the Object Browser and appears in the Quick Info window

No comments:

Post a Comment