Passing Arguments
Passing Arguments
Using Global Variables
In the previous lesson, we saw that you could declare a global variable outside of any procedure. When using various procedures in a code file, one of the characteristics of a global variable is that it is automatically accessible to other procedures. Still, a global variable can use access modifiers that would control its access:
Based on this characteristic of the procedures of a module having access to global variables of the same program, you can declare such variables and initialize or modify them in any procedure of the same code file.
Practical Learning: Using Global Variables |
Module Geometry
Private Length As Double
Private Width As Double
Private Sub GetLength()
Length = InputBox("Enter Rectangle Length:")
End Sub
Private Sub GetWidth()
Width = InputBox("Enter Rectangle Width:")
End Sub
Private Function CalculatePerimeter() As Double
CalculatePerimeter = (Length + Width) * 2
End Function
Public Function Main() As Integer
Dim Perimeter As Double
GetLength()
GetWidth()
Perimeter = CalculatePerimeter()
MsgBox("=-= Square Characteristics=-=" & vbCrLf &
"Length: " & vbTab & Length & vbCrLf &
"Width: " & vbTab & Width & vbCrLf &
"Perimeter: " & Perimeter)
Return 0
End Function
End Module
Introduction to Arguments |
So far, to use a value in a procedure, we had to declare it. In some cases, a procedure may need an external value in order to carry its assignment. A value that is supplied to a procedure is called an argument.
When creating a procedure that will use an external value, declare the argument that represents that value between the parentheses of the procedure. For a procedure, the syntax you use would be:
Sub ProcedureName(Argument) End Sub
If you are creating a function, the syntax would be:
Function ProcedureName(Argument) As DataType Function Sub
The argument must be declared as a normal variable, omitting the Dim keyword. Here is an example that creates a function that takes a string as argument:
Function CalculatePayroll(strName As String) As Double Function Sub
A certain procedure can take more than one argument. In this case, in the parentheses of the procedure, separate the arguments with a comma. Here is an example of a procedure that takes two arguments:
Sub EvaluateInvoice(EmplName As String, HourlySalary As Currency) End Sub
In the body of a procedure that takes one or more arguments, use the argument(s) as you see fit as if they were locally declared variables. For example, you can involve them with values inside of the procedure. You can also exclusively use the values of the arguments to perform the assignment.
Calling a Procedure With Argument |
To call a procedure that takes an argument, type its name and a space, followed by a value for each argument between parentheses. The value provided for an argument is also called a parameter. If there is more than one argument, separate them with a comma. Here is an example:
Module Exercise Private Function GetFullName$(strFirst As String, strLast As String) Dim FName As String FName = strFirst & " " & strLast GetFullName = FName End Function Public Function Main() As Integer Dim FirstName, LastName As String Dim FullName As String Dim ComputerLanguage As String = "Visual Basic" FirstName = inputbox("Enter First Name: ") LastName = inputbox("Enter Last Name: ") FullName = GetFullName(FirstName, LastName) msgbox("Hello, " & FullName) Welcome(ComputerLanguage) Return 0 End Function Sub Welcome(ByVal strLanguage As String) msgbox("Welcome to the wonderful world of " & strLanguage) End Sub End Module
As mentioned previously, you can also use the Call keyword to call a procedure.
When you call a procedure that takes more than one argument, you must provide the values of the arguments in the exact order they are listed inside of the parentheses of the function. Fortunately, you don't have to. If you know the names of the arguments, you can type them in any order and provide a value for each. To do that, on the right side of each argument, type the := operator followed by the desired value for the argument. Here are examples:
Public Module Exercise Private Function GetFullName$(MI As String, LastName As String, FirstName As String) GetFullName = FirstName & " " & MI & " " & LastName End Function Public Function Main() As Integer Dim FullName As String Dim ComputerLanguage As String = "VBasic" FullName = GetFullName(LastName:="Roberts", FirstName:="Alan", MI:="R.") MsgBox("Hello " & FullName) Call Welcome(ComputerLanguage) Return 0 End Function Private Sub Welcome(ByVal strLanguage As String) MsgBox("Welcome to the wonderful world of " & strLanguage) End Sub End Module
Practical Learning: Passing Arguments to a Procedure |
Module Geometry Private Function GetValue(TypeOfValue As String) As Double Dim Value As Double Value = InputBox("Enter the " & TypeOfValue & ":") Return Value End Function Private Function CalculatePerimeter(ByVal Length As Double, ByVal Width As Double) As Double CalculatePerimeter = (Length + Width) * 2 End Function Public Function Main() As Integer Dim L As Double, W As Double Dim Perimeter As Double L = GetValue("Length") W = GetValue("Width") Perimeter = CalculatePerimeter(L, W) MsgBox("=-= Square Characteristics=-=" & vbCrLf & "Length: " & L & vbCrLf & "Width: " & W & vbCrLf & "Perimeter: " & Perimeter) Return 0 End Function End Module
Based on this characteristic of the procedures of a module having access to global variables of the same program, you can declare such variables and initialize or modify them in any procedure of the same code file.
So far, to use a value in a procedure, we had to declare it. In some cases, a procedure may need an external value in order to carry its assignment. A value that is supplied to a procedure is called an argument. When creating a procedure that will use an external value, declare the argument that represents that value between the parentheses of the procedure. For a sub procedure, the syntax you use would be: Sub ProcedureName(Argument) End Sub If you are creating a function, the syntax would be: Function ProcedureName(Argument) As DataType Function Sub The argument must be declared as a normal variable, omitting the Dim keyword. Here is an example that creates a function that takes a string as argument: Function CalculatePayroll(strName As String) As Double Function Sub A certain procedure can take more than one argument. In this case, in the parentheses of the procedure, separate the arguments with a comma. Here is an example of a sub procedure that takes two arguments: Sub EvaluateInvoice(EmplName As String, HourlySalary As Currency) End Sub In the body of a procedure that takes one or more arguments, use the argument(s) as you see fit as if they were locally declared variables. For example, you can involve them with values inside of the procedure. You can also exclusively use the values of the arguments to perform the assignment.
To call a procedure that takes an argument, type its name and a space, followed by a value for each argument between parentheses. The value provided for an argument is also called a parameter. If there is more than one argument, separate them with a comma. Here is an example: Module Exercise Private Function GetFullName$(strFirst As String, _ strLast As String) Dim FName As String FName = strFirst & " " & strLast GetFullName = FName End Function Public Function Main() As Integer Dim FirstName, LastName As String Dim FullName As String Dim ComputerLanguage As String = "Visual Basic" FirstName = inputbox("Enter First Name: ") LastName = inputbox("Enter Last Name: ") FullName = GetFullName(FirstName, LastName) msgbox("Hello, " & FullName) Welcome(ComputerLanguage) Return 0 End Function Sub Welcome(ByVal strLanguage As String) msgbox("Welcome to the wonderful world of " & strLanguage) End Sub End Module As mentioned previously, you can also use the Call keyword to call a procedure. When you call a procedure that takes more than one argument, you must provide the values of the arguments in the exact order they are listed inside of the parentheses of the function. Fortunately, you do not have to. If you know the names of the arguments, you can type them in any order and provide a value for each. To do that, on the right side of each argument, type the := operator followed by the desired value for the argument. Here is an example: Public Module Exercise Private Function GetFullName$(MI As String, _ LastName As String, _ FirstName As String) GetFullName = FirstName & " " & MI & " " & LastName End Function Public Function Main() As Integer Dim FullName As String Dim ComputerLanguage As String = "VBasic" FullName = GetFullName(LastName:="Roberts", FirstName:="Alan", MI:="R.") MsgBox("Hello " & FullName) Call Welcome(ComputerLanguage) Return 0 End Function Private Sub Welcome(ByVal strLanguage As String) MsgBox("Welcome to the wonderful world of " & strLanguage) End Sub End Module
|
|
|||||||||||||||||||||
|
When calling a procedure that takes an argument, we were supplying a value for that argument. When this is done, the procedure that is called makes a copy of the value of the argument and makes that copy available to the calling procedure. That way, the argument itself is not accessed. This is referred to as passing an argument by value. This can be reinforced by typing the ByVal keyword on the left side of the argument. Here is an example: Private Sub Welcome(ByVal strLanguage As String) MsgBox("Welcome to the wonderful world of " & strLanguage) End Sub If you create a procedure that takes an argument by value and you have used the ByVal keyword on the argument, when calling the procedure, you do not need to use the ByVal keyword; just the name of the argument is enough, as done in the examples on arguments so far. Here is an example: Public Module Exercise Public Function Main() As Integer Dim ComputerLanguage As String = "Visual Basic" Welcome(ComputerLanguage) Return 0 End Function Private Sub Welcome(ByVal strLanguage As String) MsgBox("Welcome to the wonderful world of " & strLanguage) End Sub End Module This would produce: An alternative to passing arguments as done so far is to pass the address of the argument to the called procedure. When this is done, the called procedure does not receive a simple copy of the value of the argument: the argument is accessed by its address. That is, at its memory address. With this technique, any action carried on the argument will be kept. If the value of the argument is modified, the argument would now have the new value, dismissing or losing the original value it had. This technique is referred to as passing an argument by reference. Consider the following program: Public Module Exercise Private Function Addition#(ByVal Value1 As Double, ByVal Value2 As Double) Value1 = InputBox("Enter First Number: ") Value2 = InputBox("Enter Second Number: ") Addition = Value1 + Value2 End Function Public Function Main() As Integer Dim Result As String Dim Number1, Number2 As Double Result = Addition(Number1, Number2) MsgBox(Number1 & " + " & Number2 & " = " & Result) Return 0 End Function End Module Here is an example of running the program: Notice that, although the values of the arguments were changed in the Addition() procedure, at the end of the procedure, they lose the value they got in the function. If you want a procedure to change the value of an argument, you can pass the argument by reference. To pass an argument by reference, on its left, type the ByRef keyword. This is done only when creating the procedure. When the called procedure finishes with the argument, the argument would keep whatever modification was made on its value. Now consider the same program as above but with arguments passed by reference: Public Module Exercise Private Function Addition#(ByRef Value1 As Double, ByRef Value2 As Double) Value1 = InputBox("Enter First Number: ") Value2 = InputBox("Enter Second Number: ") Addition = Value1 + Value2 End Function Public Function Main() As Integer Dim Result As String Dim Number1, Number2 As Double Result = Addition(Number1, Number2) MsgBox(Number1 & " + " & Number2 & " = " & Result) Return 0 End Function End Module Here is an example of running the program:
Using this technique, you can pass as many arguments by reference and as many arguments by value as you want. As you may guess already, this technique is also used to make a sub procedure return a value, which a regular sub routine cannot do. Furthermore, passing arguments by reference allows a procedure to return as many values as possible while a regular function can return only one value.
|
|
||||||||||
|
|
|||
Previous | Copyright © 2008-2024, FunctionX, Inc. | Monday 14 February 2016 | Next |
|