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.
|
|
||||||||||
|