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: ... 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, 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? You could have the sub return a list of values. Code: BEGIN Tom L Cui bono? |
|||
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. |
|||
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:
Stephen Lewkowicz (G1CMZ) https://my.numworks.com/python/steveg1cmz |
|||
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 |
|||
10-30-2017, 10:51 AM
Post: #6
|
|||
|
|||
RE: Sub-routine returning multiple values
(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: ... |
|||
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:
Stephen Lewkowicz (G1CMZ) https://my.numworks.com/python/steveg1cmz |
|||
10-31-2017, 02:38 AM
Post: #8
|
|||
|
|||
RE: Sub-routine returning multiple values
Thanks to all who replied.
|
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)