Arguments and Parameters |
|
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 routine, 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 only 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 routine 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.
|
|
Passing Arguments (By Value) |
Practical Learning: Passing Arguments by Value |
|
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 doesn't receive a simple copy of the value of the argument: the argument is accessed at its root. 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: Module Module1 Function Addition#(ByVal Value1 As Double, ByVal Value2 As Double) Console.Write("Enter First Number: ") Value1 = Console.ReadLine() Console.Write("Enter Second Number: ") Value2 = Console.ReadLine() Addition = Value1 + Value2 End Function Sub Main() Dim Result As String Dim Number1, Number2 As Double Result = Addition(Number1, Number2) Console.Write("{0} + {1} = {2}", Number1, Number2, Result) Console.WriteLine() End Sub End Module Here is an example of running the program: Enter First Number: 224.58 Enter Second Number: 6068.14 0 + 0 = 6292.72 Notice that, although the values of the arguments were changed in 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 function. 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: Module Module1 Function Addition#(ByRef Value1 As Double, ByRef Value2 As Double) Console.Write("Enter First Number: ") Value1 = Console.ReadLine() Console.Write("Enter Second Number: ") Value2 = Console.ReadLine() Addition = Value1 + Value2 End Function Sub Main() Dim Result As String Dim Number1, Number2 As Double Result = Addition(Number1, Number2) Console.Write("{0} + {1} = {2}", Number1, Number2, Result) Console.WriteLine() End Sub End Module Here is an example of running the program: Enter First Number: 224.58 Enter Second Number: 6068.14 224.58 + 6068.14 = 6292.72 Using this technique, you can pass as many arguments by reference and as many arguments by value as you want. As you may 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. |
Practical Learning: Passing Arguments by Reference |
|
|
||
Previous | Copyright © 2004-2007 FunctionX, Inc. | Next |
|