Solved: Check if function has been called as sub
Functions can be called as subs, in other words, without the calling code making use of the parameter; in yet other words, not on the right side of an = sign. I feel a function should know if that is the case. For instance, wouldn't it be nice and elegant if we could write the following?
Code:
Function ConcatStrings _
(ByRef s1 As String, ByVal s2 As String, Optional ByVal sSeparator As String = " ") _
As String
ConcatStrings = IIf(s1 = "", s2, s1 & sSeparator & s2)
If I´mASub Then s1 = ConcatStrings ' Don't change the parameter unnecessarily.
End Function
Where I´mASub is something Boolean (a hidden parameter, or a function) that tells the function whether it has been called as a sub. Is there anything like that?
Check if function has been called as sub - a decade on
Quote:
Originally Posted by
Sebastian H
Functions can be called as subs, in other words, without the calling code making use of the parameter; in yet other words, not on the right side of an = sign. I feel a function should know if that is the case. For instance, wouldn't it be nice and elegant if we could write the following?
[vba]Function ConcatStrings _
(ByRef s1 As String, ByVal s2 As String, Optional ByVal sSeparator As String = " ") _
As String
ConcatStrings = IIf(s1 = "", s2, s1 & sSeparator & s2)
If I´mASub Then s1 = ConcatStrings ' Don't change the parameter unnecessarily.
End Function
[/vba] Where I´mASub is something Boolean (a hidden parameter, or a function) that tells the function whether it has been called as a sub. Is there anything like that?
Quote:
Originally Posted by
Bob Phillips
Why do you think this, what situation would this be useful for?
Quote:
Originally Posted by
Sebastian H
The point is that it would allow me to write clean code; code that avoids unpleasant surprises.
However, this is beside the point of my question. My intention was to find out if such functionality exists. I guess my question has been answered now; if an experienced mentor such as you hasn’t heard of it, then it probably doesn’t exist. So, thank you for taking the time to think about it. I’ll leave the question active for a few more days, but if no one else has heard of that functionality, then I’ll close this as resolved.
Came across this a decade later because I have the same need, and the lack of info from google searches suggest it isn't possible, but no harm asking right? :)
The OP wanted to know if it was possible to detect whether a caller was invoking a function as a function or as a sub i.e. x=fn(a,b) compared to fn a,b. I have a use case for this where if a routine is called as a function I want to open a form as modal, not letting any other processing occur until the form is finished with, and return the results of the form processing. If it is called as a sub I will open the form modelessly, the user can go about their other business and I will raise an event with the results when the form is closed. I don't want to raise the event if it is called as a function.
My current code can handle this but it relies on me sending an additional argument to the function just for this purpose (I send "return" if I want it to return a value).
Anyone know of a way to detect if a function is called as a function or just as a sub?