How do I create a function call w/variable no. of params?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
How do I create a function call w/variable no. of params?
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
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
-
- 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?
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.
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 395 times
Re: How do I create a function call w/variable no. of params?
Another way you can do it is by using the params() and paramCount() functions. Here's a simple example:
Hope this helps...
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
Ken Ray
Sons of Thunder Software
Email: kray@sonsothunder.com
Web site: http://www.sonsothunder.com
Sons of Thunder Software
Email: kray@sonsothunder.com
Web site: http://www.sonsothunder.com
Re: How do I create a function call w/variable no. of params?
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
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
Re: How do I create a function call w/variable no. of params?
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.
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
HyperActive Software | http://www.hyperactivesw.com
Re: How do I create a function call w/variable no. of params?
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
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
Re: How do I create a function call w/variable no. of params?
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:
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.
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
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: How do I create a function call w/variable no. of params?
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.
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
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.

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
Re: How do I create a function call w/variable no. of params?
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.)
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
HyperActive Software | http://www.hyperactivesw.com
-
- 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?
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!
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!