Arguments and Parameters |
|
Passing Arguments |
Local and Global Variables |
We know that you could declare a global variable outside of any procedure. When using various procedures in a module, one of the characteristics of a global variable is that it is automatically accessible to other procedures: |
Based on this characteristic of the procedures of a module having access to global variables of the same module, you can declare such variables and initialize or modify them in any procedure of the same code file. |
Here is an example: Option Explicit Private Length As Double Private Width As Double Private Sub GetLength() Length = 48.24 End Sub Private Sub GetWidth() Width = 25.82 End Sub Private Function CalculatePerimeter() As Double GetLength GetWidth CalculatePerimeter = (Length + Width) * 2 End Function
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 While a certain procedure can take one argument, another 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.
The value provided for an argument is also called a parameter. To call a procedure that takes an argument, type its name. Then you have various options to access its argument(s). Earlier, we saw that, to call a procedure, you could just use its name. After the name of the procedure, you can type the opening parenthesis "(", followed by the name of the argument, followed by =, and the value of the argument. If the procedure takes more than one argument, separate them with commas. Here is an example: Private Function GetFullName$(First As String, Last As String) Dim FName As String FName = First & Last GetFullName = FName End Function Sub Exercise() Dim FirstName As String, LastName As String Dim FullName As String FirstName = "Patricia " LastName = "Katts" FullName = GetFullName(FirstName, LastName) ActiveCell.FormulaR1C1 = FullName End Sub 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 order they are listed inside of the parentheses. 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 this, in the parrentheses of the procedure you are calling, type the name of the argument whose value you want to specify, followed by the := operator, and followed by the desired value for the argument. Here is an example: Private Function GetFullName$(First As String, Last As String) Dim FName As String FName = First & Last GetFullName = FName End Function Sub Exercise() Dim FullName$ FullName$ = GetFullName(Last:="Roberts", First:="Alan ") ActiveCell.FormulaR1C1 = FullName End Sub The above technique we have just seen for using the parentheses is valid for sub procedures and functions. If the procedure you are calling is a sub, you can omit the parentheses. If calling a sub procedure, after the name of the procedure, put an empty space, followed by the name of the argument assigned the desired value. Here is an example: Private Sub ShowResult(ByVal Result As Double) Result = 145.85 End Sub Public Sub Exercise() Dim Number As Double ShowResult Number End Sub If the sub procedure is taking more than one argument, separate them with commas.
|
|
||
Previous | Copyright © 2008-2016, FunctionX, Inc. | Home |
|