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: ```...   my_vars:=Sub_Routine(a,b,c);   d:=my_vars(1);   e:=my_vars(2);   f:=my_vars(3); ... Sub_Routine(x,y,z) BEGIN   RETURN({x+1,y+2,z+3}); END;``` 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? 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: ```...   my_vars:=Sub_Routine(a,b,c);   d:=my_vars(1);   e:=my_vars(2);   f:=my_vars(3); ... Sub_Routine(x,y,z) BEGIN   RETURN({x+1,y+2,z+3}); END;``` 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? You could have the sub return a list of values. Code: ```BEGIN   LOCAL mylist;   mylist:=MAKELIST(0,C,1,3);    mylist(1):=x;   mylist(2):=y+2;   mylist(3):=z+3;   RETURN mylist; END;``` 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: ```  GetCoordinates()  Begin  Return {0,0}  End; Myprog() Begin Local latitude, Longitude; {Latitude,Longitude} := GetCoordinates(); End``` But that is not in the current syntax. 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:   Code: ```...   my_vars:=Sub_Routine(a,b,c);   d:=my_vars(1);   e:=my_vars(2);   f:=my_vars(3); ... Sub_Routine(x,y,z) BEGIN   RETURN({x+1,y+2,z+3}); END;``` 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? You can reduce the four lines to one with: Code: ```...   sto(Sub_Routine(a,b,c),{'d','e','f'}); ...``` It's shorter but slower due to the call to a CAS function (sto). In a CAS program you should be able to do: Code: ```...   {d,e,f}:=Sub_Routine(a,b,c); ...``` 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: ```   COORD() BEGIN RETURN {45,54}; END;  EXPORT TRYL()  BEGIN    LOCAL LAT,LON;   PRINT();   sto(COORD(),{'LAT','LON'});   PRINT(LAT);  PRINT(LON); END;``` RE: Sub-routine returning multiple values - Jacob Wall - 10-31-2017 02:38 AM Thanks to all who replied.