Post Reply 
Sub-routine returning multiple values
10-30-2017, 12:48 AM
Post: #1
Sub-routine returning multiple values
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?
Visit this user's website Find all posts by this user
Quote this message in a reply
10-30-2017, 01:14 AM
Post: #2
RE: Sub-routine returning multiple values
(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;

Tom L

DM42 SN: 00025
Find all posts by this user
Quote this message in a reply
10-30-2017, 06:08 AM
Post: #3
RE: Sub-routine returning multiple values
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

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
Find all posts by this user
Quote this message in a reply
10-30-2017, 09:00 AM
Post: #4
RE: Sub-routine returning multiple values
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.

Stephen Lewkowicz (G1CMZ)
List API V1.1: http://www.hpmuseum.org/forum/thread-9411.html
Visit this user's website Find all posts by this user
Quote this message in a reply
10-30-2017, 09:15 AM
Post: #5
RE: Sub-routine returning multiple values
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
Find all posts by this user
Quote this message in a reply
10-30-2017, 10:51 AM
Post: #6
RE: Sub-routine returning multiple values
(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);
...
Find all posts by this user
Quote this message in a reply
10-30-2017, 06:47 PM
Post: #7
RE: Sub-routine returning multiple values
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;

Stephen Lewkowicz (G1CMZ)
List API V1.1: http://www.hpmuseum.org/forum/thread-9411.html
Visit this user's website Find all posts by this user
Quote this message in a reply
10-31-2017, 02:38 AM
Post: #8
RE: Sub-routine returning multiple values
Thanks to all who replied.
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)