Optional Arguments in Functions?

UberDan

Member
Joined
Nov 27, 2005
Messages
18
Programming Experience
Beginner
I have two questions about the following module.

a) What TYPE should I give the function? This is my first function and Option Strict is telling me that I need an 'As' clause for it.

b) I want dblMonths to be an optional argument with a default value of 60. How might I go about this?


Module MonthlyPayment

Public Function Calculate(ByVal dblAmount As Double, ByVal dblRate As Double, ByVal dblMonths As Double)
Dim dblPayment As Double
dblPayment = dblAmount * (dblRate / 12) / (1 - 1 / (1 + dblRate / 12) ^ dblMonths)
Return dblPayment
End Function

End Module
 
a.
Add the "as double" to the end of your function.

Public Function Calculate(ByVal dblAmount As Double, ByVal dblRate As Double, ByVal dblMonths As Double) As Double
Dim dblPayment As Double
dblPayment = dblAmount * (dblRate / 12) / (1 - 1 / (1 + dblRate / 12) ^ dblMonths)
Return dblPayment
End Function

b.
Make dblMonths a modular variable and use it as needed
At the top of your code:
Dim mdblMonths as double = 60 'The "m" tells you it's modular
 
While the modular variable *might* work... it's not an ideal solution.

With .NET we can introduce the idea of overloading... this is one prime case.

VB.NET:
Public Function Calculate(ByVal dblAmount As Double, ByVal dblRate As Double) As Double
Return Calculate (dblAmount, dblRate, 60)
End Function

Public Function Calculate(ByVal dblAmount As Double, ByVal dblRate As Double, ByVal dblMonths As Double) As Double
Dim dblPayment As Double
dblPayment = dblAmount * (dblRate / 12) / (1 - 1 / (1 + dblRate / 12) ^ dblMonths)
Return dblPayment
End Function

When you call calculate with out the months parameter, it will call the overloaded Calculate and pass in 60 as the value for Months.

When you need to call it with months, then you simply call the overloaded one- which is as simple as including the parameter. VB.NET will figure out the right one to call.

-tg

-tg
 
TechGnome said:
While the modular variable *might* work... it's not an ideal solution.

With .NET we can introduce the idea of overloading... this is one prime case.

VB.NET:
Public Function Calculate(ByVal dblAmount As Double, ByVal dblRate As Double) As Double
Return Calculate (dblAmount, dblRate, 60)
End Function

Public Function Calculate(ByVal dblAmount As Double, ByVal dblRate As Double, ByVal dblMonths As Double) As Double
Dim dblPayment As Double
dblPayment = dblAmount * (dblRate / 12) / (1 - 1 / (1 + dblRate / 12) ^ dblMonths)
Return dblPayment
End Function
When you call calculate with out the months parameter, it will call the overloaded Calculate and pass in 60 as the value for Months.

When you need to call it with months, then you simply call the overloaded one- which is as simple as including the parameter. VB.NET will figure out the right one to call.

-tg

-tg
Thanks man, this is exactly what I believe my prof is looking for. We did briefly cover overloading, but I didn't quite grasp the concept of it and what it's used for. This makes it a lot clearer.
 
u shld also know abt optional arguments

VB.NET:
sub show message(OPTIONAL byVal strText as String="Hello!")
----------
end sub

RULES
arguments after/following an optional argument in argument list must also be optional arguments.
 
aniskhan said:
u shld also know abt optional arguments

VB.NET:
sub show message(OPTIONAL byVal strText as String="Hello!")
----------
end sub
RULES
arguments after/following an optional argument in argument list must also be optional arguments.
I don't quite understand how that works.
 
i think using the Optional keyword would work better in this case:
VB.NET:
Public Function Calculate(ByVal dblAmount As Double, ByVal dblRate As Double, Optional ByVal dblMonths As Double = 60) As Double
  Return dblAmount * (dblRate / 12) / (1 - 1 / (1 + dblRate / 12) ^ dblMonths)
End Function

by using optional, it means that a "default" value is assigned if the sub calling the function does not provide the parameter
but it only works best if you only have one optional parameter, if there are two or more then you should be making overloaded functions
 
Last edited:
JuggaloBrotha said:
i think using the Optional keyword would work better in this case:
VB.NET:
Public Function Calculate(ByVal dblAmount As Double, ByVal dblRate As Double, Optional ByVal dblMonths As Double = 60) As Double
  Return dblAmount * (dblRate / 12) / (1 - 1 / (1 + dblRate / 12) ^ dblMonths)
End Function

by using optional, it means that a "default" value is assigned if the sub calling the function does not provide the parameter
but it only works best if you only have one optional parameter, if there are two or more then you should be making overloaded functions
Ok, thanks... that makes more sense. I think that's actually what he wants.
 
Last edited by a moderator:
some small suggestions:

i would give the function a more meaningful name than just "Calculate", something like "CalcInterest" or "CalculateInterest" since it looks like an interest calculation for a loan

also your math as a flaw in it:
dblAmount * (dblRate / 12) / (1 - 1 / (1 + dblRate / 12) ^ dblMonths)

that'll always be 0 (zero), why not just put 0 instead of 1-1
 
JuggaloBrotha said:
some small suggestions:

i would give the function a more meaningful name than just "Calculate", something like "CalcInterest" or "CalculateInterest" since it looks like an interest calculation for a loan

also your math as a flaw in it:
dblAmount * (dblRate / 12) / (1 - 1 / (1 + dblRate / 12) ^ dblMonths)

that'll always be 0 (zero), why not just put 0 instead of 1-1
Thanks for the suggestions. I will rename the function, however the math is not flawed. Remember your BEDMAS. Division comes before subtraction
 
For everyone's information:

Optional arguments are a VB-specific feature. If your code might be called from another language, like a C# app using your VB.NET library, then a value must always be provided for all arguments, thus overloading is more appropriate in that case. It is appropriate to use optional arguments if you always want the argument(s) to have a value that will get used. It is not appropriate to use optional arguments if the default value indicates that the argument will not be used. Note also that optional arguments can only be of an in-built type (String, Integer, Boolean, etc.) because you must provide a constant value as the default. Because of these inconsistencies, I believe it is best to stick to overloading in all cases, but if that's what your professor wants then obviously you need to give it to him in this case.
 
Back
Top