cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Barvaz
Level 6

How can i Pass an optional parameter to a subroutine

Hi,

I Want to write a function that can received different number of parameters, f.e:

prototype trace(HWND, STRING,NUMBER);

function trace(hMSI, message, flag)
begin
if (flag = 1) then
MessageBox(message,INFORMATION);

else
MessageBox("111111" + message,INFORMATION);

endif;

end;


trace(hMSI,"test",1);
trace(hMSI,"test");

How can i write it?
Thanks
Labels (1)
0 Kudos
(5) Replies
restrain64
Level 3

I am not sure what you are asking. But is this what your looking for?

prototype trace(HWND, STRING,STRING);

function trace(hMSI, AddStr, message)
STRING svNewStr;
begin

if (StrLength(AddStr) > 0 ) then
svNewStr = AddStr + message;
else
svNewStr = message;
endif;
MessageBox(svNewStr,INFORMATION);

end;

trace(hMSI,"1111111","test");
trace(hMSI,"", "test");
0 Kudos
Barvaz
Level 6

Yes and no 🙂
I want/need that the second parameter will be optional - Can i call to function like this: trace(hMSI,"test");
or if the second parameter is empty i must to call like this: trace(hMSI,"test","");

the function always get Address and the message is optional.

Thanks.
0 Kudos
restrain64
Level 3

You will have to call the function with the empty string. I do not think you can create a function that takes optional input. You can in C or C++. The only function in installscript that works that way is Sprinft or SprintfBox.
0 Kudos
Barvaz
Level 6

So if you can send empty value to Sprinft and SprintfBox functions - you can do it to your private functions as well, no?

I want to know if the following 2 marked options can be used:

prototype trace(HWND, STRING,STRING);

function trace(hMSI, message, AddStr)
STRING svNewStr;

begin

if (StrLength(AddStr) > 0 ) then
svNewStr = AddStr + message;

else
svNewStr = message;

endif;
MessageBox(svNewStr,INFORMATION);


end;

trace(hMSI,"test","111111");
trace(hMSI,"test");
0 Kudos
RobertDickau
Flexera Alumni

I think you can have variable arguments by including ... in a function prototype, and then referring to whatever matches the ... as an array of VARIANT-type items. That's apparently what the Sprintf[Box|MsiLog] and AskOptions functions do. This isn't the same as optional arguments, of course, and so you might be better off creating different functions, or passing null arguments when not needed as suggested.
0 Kudos