How to deal with more than one optional parameters in a function?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
diobosco
Posts: 10
Joined: Thu Nov 25, 2010 11:08 pm

How to deal with more than one optional parameters in a function?

Post by diobosco » Fri Dec 27, 2019 6:26 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: How to deal with more than one optional parameters in a function?

Post by dunbarx » Sat Dec 28, 2019 1:36 am

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:

Code: Select all

on mouseUp
   answer getParams(3,4,5)
end mouseUp

function getParams 
   return param(2)
end getParams
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

diobosco
Posts: 10
Joined: Thu Nov 25, 2010 11:08 pm

Re: How to deal with more than one optional parameters in a function?

Post by diobosco » Sat Dec 28, 2019 2:19 am

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

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: How to deal with more than one optional parameters in a function?

Post by mwieder » Sat Dec 28, 2019 2:56 am

I deal with that situation like this:

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
and then you can call myFunc as any of

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: How to deal with more than one optional parameters in a function?

Post by dunbarx » Sat Dec 28, 2019 4:30 am

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

diobosco
Posts: 10
Joined: Thu Nov 25, 2010 11:08 pm

Re: How to deal with more than one optional parameters in a function?

Post by diobosco » Sat Dec 28, 2019 3:31 pm

mwieder wrote:
Sat Dec 28, 2019 2:56 am
I deal with that situation like this:

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
and then you can call myFunc as any of

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
Thank you Marc. This helps me a lot.

Post Reply