Sub-routine returning multiple values - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html) +--- Forum: HP Prime (/forum-5.html) +--- Thread: Sub-routine returning multiple values (/thread-9394.html) |
Sub-routine returning multiple values - Jacob Wall - 10-30-2017 12:48 AM I haven't found any mention of it, but thought that it might be something already implemented? What I'm curious about is having a sub-routine calculating multiple values and returning those values back to the function that called it. Currently what I'm doing is returning a list of values form the sub-routine, then one by one assigning to the variables I want. A completely made-up example illustrating what I'm talking about: Code: ... It would be handy to be able to reduce the four lines to a single line, some like (d,e,f):=Sub_Routine(a,b,c); or something like that. I haven't fully thought this through yet, but couldn't help thinking there could be a better way. Any insights or further thought? RE: Sub-routine returning multiple values - toml_12953 - 10-30-2017 01:14 AM (10-30-2017 12:48 AM)Jacob Wall Wrote: I haven't found any mention of it, but thought that it might be something already implemented? You could have the sub return a list of values. Code: BEGIN RE: Sub-routine returning multiple values - cyrille de brébisson - 10-30-2017 06:08 AM Hello, Unfortunately, HPPL does not allow one function to return multiple values. They are functions in the mathematical sense of things and must return one single value. Now, this value can of course be a composit object such as a list that can itself contain multiple heteroclit objects. But this forces a 3 step process of combining all values in 1 list, storing this list somewhere and then decombining (if/as needed) Cyrille RE: Sub-routine returning multiple values - StephenG1CMZ - 10-30-2017 09:00 AM Since the value returned is a list, I have previously suggested that the syntax be enhanced to allow a list of variables to be assigned to: For example: Code:
RE: Sub-routine returning multiple values - webmasterpdx - 10-30-2017 09:15 AM The Miscellaneous Tips article linked to from the HP Prime wiki Programming Documentation section says this: To return multiple values, return it as a list. e.g. f1(x) returns a list containing 3 integers. The values returned can be assigned to variables like this: L:=f1(x);A:=L(1);B:=L(2);C:=L(3); http://www.wiki4hp.com/doku.php?id=prime:start RE: Sub-routine returning multiple values - Didier Lachieze - 10-30-2017 10:51 AM (10-30-2017 12:48 AM)Jacob Wall Wrote: You can reduce the four lines to one with: Code: ... In a CAS program you should be able to do: Code: ... RE: Sub-routine returning multiple values - StephenG1CMZ - 10-30-2017 06:47 PM Although Didier's solution uses a CAS sto, it can be used from PPL too, without requiring CAS variables: Code:
RE: Sub-routine returning multiple values - Jacob Wall - 10-31-2017 02:38 AM Thanks to all who replied. |