Page 1 of 1
Function parameters
Posted: Thu Jul 15, 2021 7:50 pm
by geo
Hello
I have a function which returns the values:
2600, 2599.973, 2631.284, -25.647696, -30.116531
Now I would like to use those values as parameters for this function
Code: Select all
function forecast pXactual, pX1, pX2, pY1, pY2
return (pY2 + (pXactual - pX2) / (pX1 - pX2) * (pY1 - pY2))
end forecast
When I call the function like
Code: Select all
put forecast(2600, 2599.973, 2631.284, -25.647696, -30.116531)
I get the correct result
When I call it like
it won't work
I also tried to output the values from firstFunction into a variable and into a field and call the parameters from there, no success
What am I missing?
Thanks for your help
Re: Function parameters
Posted: Thu Jul 15, 2021 8:30 pm
by SparkOut
geo wrote: ↑Thu Jul 15, 2021 7:50 pm
Hello
I have a function which returns the values:
2600, 2599.973, 2631.284, -25.647696, -30.116531
I would be interested to see
that function, and what it returns.
Normally a function can return only one value.
Are you sure it does not return one value "2600, 2599.973, 2631.284, -25.647696, -30.116531"?
You could pass that to the forecast function, and take the single argument, then within the forecast function, use each item of the argument to use in the forecast.
Or get very granular and check the number of arguments passed to the forecast function, check the number of items within the passed argument(s) and act accordingly.
Re: Function parameters
Posted: Thu Jul 15, 2021 8:41 pm
by geo
Code: Select all
return pValueToFind & comma && item 2 of bla & comma && item 4 of bla & comma && item 1 of bla & comma && item 3 of bla
This is from the first function
bla is the variable where it all comes together, so I thought I can split it up with a comma to use it directly as parameters
Re: Function parameters
Posted: Thu Jul 15, 2021 8:50 pm
by SparkOut
Yes, it is one returned value with multiple items.
In the forecast function add some lines to answer the individual arguments.
You should find that pXactual answers "2600, 2599.973, 2631.284, -25.647696, -30.116531" and the rest empty.
So in the forecast function, check for the number of items of pXactual and whether the others are empty. If the number of items of pXactual is more than 1, then you should be able to take item 2 of that to use for px1, etc.
Re: Function parameters
Posted: Thu Jul 15, 2021 10:43 pm
by stam
geo wrote: ↑Thu Jul 15, 2021 7:50 pm
it won't work
As Sparkout says, your firstFunction() can
only return a single value (a comma-delimited list of values in this case).
Therefore your forecast() function needs to parse out the parameters from the list returned by firstFunction() and then use them.
or to put it differently, your forecast() function is expecting 5 parameters but only receiving one (the list returned by firstFunction). The list you get out of firstFunction() is seen as pXactual by forecast(), with all other parameters empty.
If you jut wanted to have the 1 function to cater both for passing 5 values directly and for cases where a single list (of 5 values) is passed back, just check that your parameters are not empty and act accordingly
eg:
Code: Select all
function forecast pXactual, pX1, pX2, pY1, pY2
local tXactual, tX1, tX2, tY1, tY2
if pX1 is empty and pX2 is empty and pY1 is empty and pY2 is empty then
//assume the value passed as pXactual is in fact a list of the 5 values in the order specified
put item 1 of pXactual into tXactual
put item 2 of pXactual into tX1
put item 3 of pXactual into tX2
put item 4 of pXactual into tY1
put item 5 of pXactual into tY2
return (tY2 + (tXactual - tX2) / (tX1 - tX2) * (tY1 - tY2))
else
return (pY2 + (pXactual - pX2) / (pX1 - pX2) * (pY1 - pY2))
end if
end forecast
not tested, but would expect this to work

This does however rely on the order of the list of the list being correct. You may wish to return an array from firstFunction() so that there is no ambiguity...
Re: Function parameters
Posted: Fri Jul 16, 2021 5:18 am
by geo
Thanks guys, that solved the problem
Re: Function parameters
Posted: Fri Jul 16, 2021 8:36 pm
by jacque
Actually, you can do it the way you originally tried if you don't concatenate the variable values. Using "&" creates a single string, but sending each piece separated by commas will populate all the parameters because LC evaluates each item before sending it to the function:
Code: Select all
get forecast(item 1 of bla, item 2 of bla, item 3 of bla, item 4 of bla, item 5 of bla)