6

I'd like to define a function in Visual Basic which computes income tax in a given bracket. The inputs should be the income, the marginal tax rate, the lower bracket boundary, and - optionally - the upper bracket boundary. (For the top bracket there is no upper boundary).

Here's how I went about it. First, I define a "ramp" function as follows:

Public Function ramp(x)
    ramp = (x + Abs(x)) / 2
End Function

which is basically the same as IF(x<0,0,x). Then I define the function (in Dutch) for the tax as

Public Function schijfbelasting(inkomen, ondergrens, bovengrens, tarief)
    schijfbelasting = ramp(tarief * (inkomen - ondergrens)) - ramp(tarief * (inkomen - bovengrens))
End Function

Here "inkomen"=income, "ondergrens"=lower bracket boundary, "bovengrens"=upper bracket boundary, "tarief"=marginal tax rate, and the output "schijfbelasting"=tax in the specified bracket.

This all works fine, except that I'd like to make the "bovengrens" (upper bracket boundary) optional using

Optional bovengrens

In Matlab, I would use the "nargin" (number of arguments) function to do something like the following:

Public Function schijfbelasting(inkomen, ondergrens, Optional bovengrens, tarief)
If nargin==4
    schijfbelasting = ramp(tarief * (inkomen - ondergrens)) - ramp(tarief * (inkomen - bovengrens))
Elseif nargin==3
schijfbelasting = ramp(tarief*(inkomen-ondergrens))
End If
End Function

However, I'm not aware of a function similar to "nargin" in Visual Basic. It could also be something like "if the argument "bovengrens" is defined". Does anybody know how to approach this problem? Thanks in advance.

P.S. I am aware that I can make the code 'work' by filling in a very large number for the bracket 'boundary' in the top bracket, but I do not consider this elegant coding.

1
  • 3
    if you use Optional parameter in function definition all parameters which follow after first Optional have to be Optional, too. So, either move Optional bovengrens to the end of parameter list or make Optional tarief. too. Commented Jun 27, 2013 at 19:10

2 Answers 2

7

You can use VBA's IsMissing function to test for optional parameters. Here's an example:

Public Sub OptionalArg(arg1, Optional arg2)
Debug.Print arg1; IIf(IsMissing(arg2), "missing", arg2)
End Sub

Test it like this:

Sub Test()
OptionalArg 1
OptionalArg 1, 2
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Might also be good to specify type as Variant an implicit conversion might cause an error? Public Sub OptionalArg(arg1 as Variant, Optional arg2 as Variant) It's been a while since I've used optionals but excel/vba is notorious for doing whatever it wants with data types.
1

Just test the arguments using the IsMissing function:

If IsMissing(bovengrens) Then ...

2 Comments

Much better than your original answer :)
@DougGlancy Heh, yeah, I always use a default of Nothing for optional variant parameters, so I forgot ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.