If a procedure takes more than one argument, you can provide a default argument for each and select which ones would have default values. If you want all arguments to have default values, when defining the procedure , provide the Optional keyword for each and assign it the desired default value. Here is an example: Public Module Exercise Function CalculateNetPrice#(Optional ByVal Tax As Double = 5.75, _ Optional ByVal Discount As Double = 25, _ Optional ByVal OrigPrice As Double = 245.55) Dim DiscountValue As Double = OrigPrice * Discount / 100 Dim TaxValue As Double = Tax / 100 Dim NetPrice As Double = OrigPrice - DiscountValue + TaxValue Dim Result As String Result = "Original Price: " & vbTab & CStr(OrigPrice) & vbCrLf & _ "Discount Rate: " & vbTab & CStr(Discount) & "%" & vbCrLf & _ "Tax Amount: " & vbTab & CStr(Tax) MsgBox(Result) Return NetPrice End Function Public Function Main() As Integer Dim FinalPrice As Double FinalPrice = CalculateNetPrice() MsgBox("Final Price: " & CStr(FinalPrice)) Return 0 End Function End Module This would produce: If a procedure takes more than one argument as above, remember that some arguments can be specified as optional. In this case, when calling the procedure, any argument that does not have a default value must be passed with a value. When creating a procedure that takes more than one argument, the argument(s) that has(have) default value(s) must be the last in the procedure. This means that:
Because of this, when calling any procedure in the Visual Basic language, you must know what, if any, argument is optional and which one is not. If a procedure takes two arguments and one argument has a default value, when calling this procedure, you can pass only one value. In this case, the passed value would be applied on the first argument. If a procedure takes more than two arguments and two or more arguments have a default value, when calling this procedure, you can provide only the value(s) of the argument that is (are) not optional. If you want to provide the value of one of the arguments but that argument is not the first optional, you can leave empty the position(s) of the other argument(s) but remember to type a comma to indicate that the position is that of an argument that has a default value. Here is an example: Public Module Exercise Function CalculateNetPrice(ByVal AcquiredPrice As Double, _ ByVal MarkedPrice As Double, _ Optional ByVal TaxRate As Double = 5.75, _ Optional ByVal DiscountRate As Double = 25) As Double Dim DiscountAmount As Double = MarkedPrice * DiscountRate / 100 Dim TaxAmount As Double = MarkedPrice * TaxRate / 100 Dim NetPrice As Double = MarkedPrice - DiscountAmount + TaxAmount Dim Result As String Result = "Price Acquired: " & vbTab & CStr(AcquiredPrice) & vbCrLf & _ "Marked Price: " & vbTab & CStr(MarkedPrice) & vbCrLf & _ "Discount Rate: " & vbTab & CStr(DiscountRate) & "%" & vbCrLf & _ "Discount Amt: " & vbTab & CStr(DiscountAmount) & vbCrLf & _ "Tax Rate: " & vbTab & CStr(TaxRate) & "%" & vbCrLf & _ "Tax Amount: " & vbTab & CStr(TaxAmount) MsgBox(Result) Return NetPrice End Function Public Function Main() As Integer Dim FinalPrice As Double FinalPrice = CalculateNetPrice(225.55, 150.55, , 40) MsgBox("Final Price: " & CStr(FinalPrice)) Return 0 End Function End Module This would produce: |
|
|||
|