How do I create a function call w/variable no. of params?

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
minx
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 43
Joined: Tue May 31, 2011 3:47 am

How do I create a function call w/variable no. of params?

Post by minx » Thu Jun 23, 2011 1:43 am

Hello,
I'm going through the User's guide, and am intrigued by the function in section 5.4.6 "Implicit Parameters" on page 142. In that example, the function call is hard-coded. I'd like to modify it, so it can take any number of values as a user entry. But, how do I call that function, and create a variable number of arguments in my code "on the fly", depending on the user's answer?
I've created a version that rewrites the "implicit function" and does the same thing, but would also like to know how to do the other.
Thanks for your help!
Diane

Howlernator
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 18
Joined: Wed Oct 13, 2010 4:44 am

Re: How do I create a function call w/variable no. of params?

Post by Howlernator » Thu Jun 23, 2011 4:04 am

Hiya Diane,

Here's my take on your question. (attached)

It's based on using an array (which is populated via a repeat loop) as the parameter that you send to the function.

You may need to amend the repeat loop depending on your data sources. I've used standard editable fields in the example.

The script is all in the card script.

Warm regards,
Anthony.
Attachments
TestArrayFunction.zip
(1.43 KiB) Downloaded 394 times

kray
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 56
Joined: Sat Apr 08, 2006 5:28 pm
Contact:

Re: How do I create a function call w/variable no. of params?

Post by kray » Thu Jun 23, 2011 7:37 am

Another way you can do it is by using the params() and paramCount() functions. Here's a simple example:

Code: Select all

function AddEmUp
  put 0 into tTotal
  repeat with x = 1 to the paramCount
    add param(x) to tTotal
  end repeat
  return tTotal
end AddEmUp

put AddEmUp(1,2,3,4)
--> 8

put AddEmUp(10,13)
--> 23  
Hope this helps...
Ken Ray
Sons of Thunder Software
Email: kray@sonsothunder.com
Web site: http://www.sonsothunder.com

minx
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 43
Joined: Tue May 31, 2011 3:47 am

Re: How do I create a function call w/variable no. of params?

Post by minx » Thu Jun 23, 2011 7:25 pm

Hello Ken and Ray,

Thank you very much for your replies, and code samples, I appreciate it, so much.

It wasn't exactly what I was looking for however, and I think this had to do with the way I worded my question.
I wanted to use the function in the User's Guide, "as is" (verbatim), and wondered how I could formulate my call to it, without knowing the specific number of parameters to be passed in ahead of time? I'd like to be able to build the function call dynamically in my routine, so I can add the appropriate number of arguments to the call. I've tried passing in an array, but the receiving function sees it as a single additional parameter and causes an error.
Don't know if I explained this very well. And, I do have something that works, but rewrote the function, and I didn't want to do that.
Thanks for your help!
Diane

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: How do I create a function call w/variable no. of params?

Post by jacque » Fri Jun 24, 2011 12:35 am

That section in the user guide is talking about the same thing Ken mentioned. You can pass any number of arguments to a function. If you haven't specified the exact number of parameters in the function handler to receive them, the extras are still preserved anyway. Since there is no pre-specified place to put the extras, you can just retrieve them with the param() function. You don't need to know ahead of time how many arguments you will pass; just build up your list in the calling handler and pass them all. In the function, you wouldn't even need any parameters listed; you can use param(x) to get each of them. That's what Ken's example does.

His function finds out the number of arguments that were passed by using the paramcount function. Then it steps through them all in a repeat loop and, in this case, adds them up to get a total.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

minx
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 43
Joined: Tue May 31, 2011 3:47 am

Re: How do I create a function call w/variable no. of params?

Post by minx » Fri Jun 24, 2011 3:44 am

Hey Jacque! I think I don't know how to ask my question. So what I thought I'd do is paste in my code instead, so you can see what I'm trying to do. I think it's in the way I'm asking for the user input that's causing the problem. I'd like to be able to enter all the factors in a single input line. And then invoke the function. I put ??? where I didn't know what to do. It appears on the last line.
Thanks you guys! I Really appreciate the help! -- Diane

Code: Select all

function product firstFactor, secondFactor
   put firstFactor* secondFactor into theProduct
   if the paramCount > 2 then
      repeat with x=3 to the paramCount
         multiply theProduct by param(x)
      end repeat
   end if
   return theProduct
end product

on mouseUp
   put empty into varsList
   ask "enter the factors, separated by commas"
   put it into varsList
   put the number of items in varsList into itemCount
   repeat with x=1 to itemCount
      put item x of varsList into varItem[x]
   end repeat

   put product(varItem[1], varItem[2], ???)
   
end mouseUp

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: How do I create a function call w/variable no. of params?

Post by jacque » Fri Jun 24, 2011 4:21 am

I see now. We were looking at the wrong end of the problem. You're asking how to *send* an unknown number of parameters.

No need to create an array, unless you want to (which is perfectly legitimate too.) You should probably do some error checking to make sure the commas are in the user response, but assuming the user enters them correctly, then I'd just use text chunking in your function. Send the user response as a single parameter:

Code: Select all

on test
  ask "enter the factors, separated by commas"
  if it is not empty then
    put product(it) into theAnswer
  end if
end test

function product factors -- factors will be a comma-delimited list
  put item 1 of factors into theProduct
  put the number of items in factors into tItemLength
  if tItemLength > 1 then
    repeat with x=2 to tItemLength
      multiply theProduct by item x of factors
    end repeat
  end if
  return theProduct
end product
Alternately you could send the array you were making and have the function multiply the array elements instead (use: "put product(varItem)" in your calling handler.) But text is a little faster.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

minx
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 43
Joined: Tue May 31, 2011 3:47 am

Re: How do I create a function call w/variable no. of params?

Post by minx » Fri Jun 24, 2011 4:45 am

Thanks Jacque!
So in my case I couldn't use the implicit parameters (paramCount and param(x)) because of the way I was getting the input?
Because it was in a chunk?
I absolutely can see where I could cut down on extra code (like extra arrays & item counts) when using LiveCode. And I agree, I Should put in some error checking. :mrgreen:
I just wanted a calculator where I could put all the items in a single line and have it perform multiple operations. Seems handy, and simple.
Thanks everyone for your help!
Diane

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: How do I create a function call w/variable no. of params?

Post by jacque » Fri Jun 24, 2011 5:19 am

Right, it's because of the way "ask" returns its value as a single string. You could do your function using paramCount, but the calling handler gets ugly because there isn't a good way to separate out the string into individual parameters without using "do". Basically the "do" method writes a bit of script and loads the compiler, which evaluates and compiles the expression, and then executes it. There is overhead and it's overkill. It's much easier to just change the function to expect a single parameter and parse that. Faster too.

I hadn't downloaded Anthony's array example, but from the description it sounds like he did the same thing I did, only with an array instead of a string. Either method is fine, though the string method avoids one of the repeat loops (you don't have to build the array.)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Howlernator
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 18
Joined: Wed Oct 13, 2010 4:44 am

Re: How do I create a function call w/variable no. of params?

Post by Howlernator » Tue Jun 28, 2011 7:18 am

Yea,

The only reason I put the array in the example was to cater for the scenario that the variables came from a variety of sources, rather than one field.

Of course, it would be still possible to construct a simple string of these too.... it's either construct, or deconstruct - depending on optimisation.

... plus, I'm still learning about arrays.... and I reeeeeeally like 'em!

Post Reply