Arguments vs Parameters

• An argument is a variable, constant, or expression that is passed to a subprocedure.

• A parameter is a variable that receives a value passed to a subprocedure.

6. In the MasterProcedures module, enter the following GetFirst function procedure:

Function GetFirst(fullName As String) Dim space As Integer space = InStr(fullName, " ") GetFirst = Left(fullName, space - 1) End Function

The second statement in the master procedure (see step 4), first = GetFirst(full), passes the value of the full variable to the GetFirst function. This value is received by the function's parameter — fullName. To extract the first name from the user-provided fullName string, you must find the location of the space separating the first name and last name. Therefore, the function begins with a declaration of a local variable — space. The next statement uses the VBA built-in function InStr to return the position of a space character (" ") in the fullName string. The obtained number is then assigned to the variable space. Finally, the Left function is used to extract the specified number of characters (space -1) from the left side of the fullName string. The length of the first name is one character less than the value stored in the variable space. The result of the function (user's first name) is then assigned to the function's name. When Visual Basic returns to the master procedure, it places the result in the variable first.

Part I

7. In the MasterProcedures module, enter the following GetLast function procedure:

Function GetLast(fullName As String) Dim space As Integer space = InStr(fullName, " ") GetLast = Right(fullName, Len(fullName) - space) End Function

The third statement in the master procedure (see step 4), last = GetLast(fuil), passes the value of the full variable to the GetLast function. This function's purpose is to extract the user's last name from the user-supplied fullName string. The GetLast function uses the built-in Len function to calculate the total number of characters in the fullName string. The Right function extracts the specified number of characters (Len(fullName) - space) from the right side of the fullName string. The obtained string is then assigned to the function name and is stored in the variable last upon returning to the master procedure.

8. In the MasterProcedures module, enter the following DisplayLastFirst subroutine:

Sub DisplayLastFirst(firstName As String, lastName As String)

MsgBox lastName & ", " & firstName End Sub

The fourth statement in the master procedure (see step 4), Call DisplayLastFirst(first, last), calls the DisplayLastFirst subroutine and passes two arguments to it: first and last. To receive these arguments, the DisplayLastFirst subprocedure is declared with two matching parameters: firstName and lastName. Recall that different names can be used for arguments and their corresponding parameters. The DisplayLastFirst subprocedure then displays the message box showing the user's last name followed by a comma and the first name.

0 0

Post a comment