How to deal with more than one optional parameters in a function?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
How to deal with more than one optional parameters in a function?
Hi Forum!
I have a question with regards to optional parameters in a function/handler.
Calling a function with one parameter is easy: put myfunc(para1) into tDest.
Even in case that para1 is an optional parameter it doesn't create any problem. In the function script we could we could ask whether para1 is empty catching the para-less function call.
So far so good. But what happens if we have a function that can have more than one parameter which BOTH can be optional?
How do you deal with this?
I have the practical case where a function has three parameters from which one is mandantory and two could be optionaL?
What is the best practice in the LC community to deal with this scenario?
Thansk for your help
Diobosco
I have a question with regards to optional parameters in a function/handler.
Calling a function with one parameter is easy: put myfunc(para1) into tDest.
Even in case that para1 is an optional parameter it doesn't create any problem. In the function script we could we could ask whether para1 is empty catching the para-less function call.
So far so good. But what happens if we have a function that can have more than one parameter which BOTH can be optional?
How do you deal with this?
I have the practical case where a function has three parameters from which one is mandantory and two could be optionaL?
What is the best practice in the LC community to deal with this scenario?
Thansk for your help
Diobosco
Re: How to deal with more than one optional parameters in a function?
Hi.
Not sure why you want to separate "required" and "optional" parameters. "Optional" parameters should be dealt with entirely in your code, leaving the function call out of it. There is no way for LC, or anyone but you, to know which parameters are which.
You surely know that something like:
Where the parameters are not even listed, but are still passed in order and retrievable. So please tell me what would make a parameter more "required" than another. Whichever they are, they must be filtered (or ignored) by you, based on whatever context or situation exists at the time of the function call itself. Of course, code within the function handler can do that sort of work as well.
Craig
Not sure why you want to separate "required" and "optional" parameters. "Optional" parameters should be dealt with entirely in your code, leaving the function call out of it. There is no way for LC, or anyone but you, to know which parameters are which.
You surely know that something like:
Code: Select all
on mouseUp
answer getParams(3,4,5)
end mouseUp
function getParams
return param(2)
end getParams
Craig
Re: How to deal with more than one optional parameters in a function?
Hi Craig
Thanks for your response.
I had a function in mind that has a default mode when the optional parameter is not passed but could initiate a special handling once the parameter is given.
I have a function that reads some data input and can create 4 different types of arrays based on a parameter that "says" which array type the function should create. Now, one of the 4 array type is the most easiest and the standard type so to speak. The other three are special forms. I thought it would be a good idea to put the creation of the easiest array type as default making the parameter an optional one.
Diobosco
Thanks for your response.
I had a function in mind that has a default mode when the optional parameter is not passed but could initiate a special handling once the parameter is given.
I have a function that reads some data input and can create 4 different types of arrays based on a parameter that "says" which array type the function should create. Now, one of the 4 array type is the most easiest and the standard type so to speak. The other three are special forms. I thought it would be a good idea to put the creation of the easiest array type as default making the parameter an optional one.
Diobosco
Re: How to deal with more than one optional parameters in a function?
I deal with that situation like this:
and then you can call myFunc as any of
Code: Select all
function myFunc param1, param2
if param1 is empty then
put defaultValue1 into param1
end if
if param2 is empty then
put defaultValue2 into param2
end if
do_things_here_with_param1_and_param2
end myFunc
Code: Select all
put myFunc() # default param1 and default param2 into tVar1
put myFunc(x) # default param2 into tVar2
put myFunc(x, y) # no defaultsinto tVar3
put myFunc(,y) into tVar4 # default param1
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: How to deal with more than one optional parameters in a function?
Diabosco.
You already know how to do this. Again, based on the state of the program at the time of interest, either from the calling handler pass only the parameters you need to, or, in the function handler read and process only those you need.
At bottom, though, the point is not to "name" parameters as "required" or "optional", that is, do not think of them that way. Rather, using the state of the program at the time of the function call, either pass parameters that are appropriate, or load values in those parameters that are appropriate.
I would keep the parameter list the same, and load values that perform as you want. Some of those values can be flags that control the actions of the others.
Craig
You already know how to do this. Again, based on the state of the program at the time of interest, either from the calling handler pass only the parameters you need to, or, in the function handler read and process only those you need.
At bottom, though, the point is not to "name" parameters as "required" or "optional", that is, do not think of them that way. Rather, using the state of the program at the time of the function call, either pass parameters that are appropriate, or load values in those parameters that are appropriate.
I would keep the parameter list the same, and load values that perform as you want. Some of those values can be flags that control the actions of the others.
Craig
Re: How to deal with more than one optional parameters in a function?
Thank you Marc. This helps me a lot.mwieder wrote: ↑Sat Dec 28, 2019 2:56 amI deal with that situation like this:
and then you can call myFunc as any ofCode: Select all
function myFunc param1, param2 if param1 is empty then put defaultValue1 into param1 end if if param2 is empty then put defaultValue2 into param2 end if do_things_here_with_param1_and_param2 end myFunc
Code: Select all
put myFunc() # default param1 and default param2 into tVar1 put myFunc(x) # default param2 into tVar2 put myFunc(x, y) # no defaultsinto tVar3 put myFunc(,y) into tVar4 # default param1