ioSamurai ioSamurai - 4 months ago 41 Question

Does Exit Function return false?

In VB.NET on a boolean function if you run an Exit Function line will it return false?


That is correct, with the caveat that in VB the function name can also be a variable that is returned. If you've previously set that to true, it will return true.

More completely, in VB.Net, if I have a boolean function Foo() defined like so:

Public Function Foo() As Boolean

...the body of that function has an implied variable also named Foo that matches the return type of the function — Boolean in this case, but Object if the return type is omitted (you should be using Option Strict, which requires a return type). This implied variable is initialized to use the default value for that type.

If you fail to Return a value from the function, whether via Exit Function or simply by reaching the end, this implied variable is returned instead. Therefore, a Boolean function will return False if you Exit Function early without making other changes, because that is the default value in the implied variable used with the function. But you could also set that variable to True first if you wanted, and then Exit Function would cause it to return True instead.

These days people don't often use the implied variable, but there are situations where it can save you a few lines of code without costing anything in clarity.