Function parameters

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
geo
Posts: 31
Joined: Sun Aug 04, 2019 7:28 pm

Function parameters

Post by geo » 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

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

Code: Select all

put forecast(firstFunction())
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

SparkOut
Posts: 2839
Joined: Sun Sep 23, 2007 4:58 pm

Re: Function parameters

Post by SparkOut » Thu Jul 15, 2021 8:30 pm

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.

geo
Posts: 31
Joined: Sun Aug 04, 2019 7:28 pm

Re: Function parameters

Post by geo » Thu Jul 15, 2021 8:41 pm

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

SparkOut
Posts: 2839
Joined: Sun Sep 23, 2007 4:58 pm

Re: Function parameters

Post by SparkOut » Thu Jul 15, 2021 8:50 pm

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.

stam
Posts: 2640
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Function parameters

Post by stam » Thu Jul 15, 2021 10:43 pm

geo wrote:
Thu Jul 15, 2021 7:50 pm

Code: Select all

put forecast(firstFunction())
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...

geo
Posts: 31
Joined: Sun Aug 04, 2019 7:28 pm

Re: Function parameters

Post by geo » Fri Jul 16, 2021 5:18 am

Thanks guys, that solved the problem

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7215
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Function parameters

Post by jacque » Fri Jul 16, 2021 8:36 pm

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)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”