Detecting how many parameters were passed to a function
Hi,
We have a function that takes two parameters, the second one is for output value:
MyFunc(Param1,Param2)
S Param2="it's all good"
Q 1
How can we detect if the caller called the function with one, or two arguments?
Meaning, how to distinguish this call:
S Result=$$MyFunc(1)
from this call:
S Result=$$MyFunc(1,.OutVal)
Thanks!
Nael
MyFunc(Param1,Param2) Public {
if $d(Param2) write " 2 params",! else write " 1 param",! S Param2="it's all good" Q 1 }
Hi,
Thank you,
That would work if the caller is nice enough to set the second variable to null before calling the function.
But he may not do it..
So if the function is:
if $d(Param2) {
S Answer= " 2 params"
} Else {
S Answer= " 1 params"
}
S Param2="it's all good"
Q Answer
USER>w $$MyFunc^%ZWBNAEL(1)
1 params
USER>w $$MyFunc^%ZWBNAEL(1,.V)
1 params
Thank you Alexander,
I was not familiar with $stack , might be useful in various situations.
Nice!
I did not know this was possible.
Thank you!
I miss Public { } in your example
it ensures that only parameters are visible as <Private variables> or all %*
Hi Nael
Not possible, as far as I know.
You can play with $stack. Something like this:
set Result=$$MyFunc(1,.Out) quit MyFunc(Param1,Param2) write $stack($stack-1,"MCODE"),! set Param2="it's all good" quit 1 USER>d ^test set Result=$$MyFunc(1,.Out)
But here you are looking at parsing plain strings, and this is error-prone.