﻿ Detecting how many parameters were passed to a function | InterSystems
Question
· Jun 27, 2019

# 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

Discussion (7)0
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:

MyFunc(Param1,Param2)

if \$d(Param2) {
Else {
}

Param2="it's all good"

Then:

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.

w \$\$MyFunc(1),!,
\$\$MyFunc(1,.V)," V=",V,!

MyFunc(p...)