HP Forums

Full Version: Sub-routine returning multiple values
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
(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;
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
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.
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
(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);
...
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;
Thanks to all who replied.
Reference URL's