More than one (two?) procedure return value

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Cezary
User
User
Posts: 89
Joined: Sun Feb 12, 2017 2:31 pm

More than one (two?) procedure return value

Post by Cezary »

It would be very useful for me if the procedure could return two values, e.g. the result of an operation and its status (error code). This possibility exists, for example, in the Golang. I know that a procedure can return more values through the objects used as arguments, but this is less convenient because I have to declare the appropriate objects first. And often the second value returned is a simple variable. I believe that this syntax would have no effect on backward compatibility, it would only extend the possibilities. So maybe we will get something like this someday, for the benefit of language development?
User avatar
STARGÅTE
Addict
Addict
Posts: 2087
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: More than one (two?) procedure return value

Post by STARGÅTE »

Pure Basic can only return a single value because it uses the RAX/EAX register for this.
There is no change to return multiple values, without changing the whole core language.
But let me ask you, how such syntax with two return values would look like in Pure Basic?

Code: Select all

A, B = MyProcedure(C)
To my point of view, there is no disadvantage to use instead:

Code: Select all

Procedure MyProcedure(*Return.Integer, Value.i)
	*Return\i = Value
	ProcedureReturn #True
EndProcedure

Debug MyProcedure(@B, C)
Debug B
You do not need to declare an "appropriate objects first", you can use a simple variable with @
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Cezary
User
User
Posts: 89
Joined: Sun Feb 12, 2017 2:31 pm

Re: More than one (two?) procedure return value

Post by Cezary »

STARGÅTE wrote:
"Pure Basic can only return a single value because it uses the RAX/EAX register for this."

With C backend in the situation changes.

Code: Select all

A, B = MyProcedure(C)
It looks pretty good to me.

"To my point of view, there is no disadvantage to use instead:"

I did not know such a solution. Thanks for this example.
User avatar
mk-soft
Always Here
Always Here
Posts: 5401
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: More than one (two?) procedure return value

Post by mk-soft »

In all languages it is always only one return value internally (rax/eax). In object-oriented languages, the return value is whether the function was successful and the result is always passed to a Result parameter as a ByRef.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Cezary
User
User
Posts: 89
Joined: Sun Feb 12, 2017 2:31 pm

Re: More than one (two?) procedure return value

Post by Cezary »

Yes, but sometimes it's worth doing it differently than everyone else. PureBasic is a good example of this.
User avatar
NicTheQuick
Addict
Addict
Posts: 1226
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: More than one (two?) procedure return value

Post by NicTheQuick »

I really would like to have proper exception handling in Purebasic. I want to throw exceptions and then catching them somewhere later. It would make so much things more easy. And you could also see it as a secondary return value but of course it is more than that.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
skywalk
Addict
Addict
Posts: 3997
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: More than one (two?) procedure return value

Post by skywalk »

Yes to exception handling, no to multiple returns.
We have fully flexible structures and pointers to them.
Your procedures can return success or fail.
And the structures hold the stable result(s).
...If wishing...
I am more interested in functions that exist within mathematical operations.
y = myA(x().cmplx) * myB(z().cmplx)
How to handle complex math with function overloading.
See the parameter types within the call and then use the appropriate procedure.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
AZJIO
Addict
Addict
Posts: 1360
Joined: Sun May 14, 2017 1:48 am

Re: More than one (two?) procedure return value

Post by AZJIO »

Code from the help file

Code: Select all

Structure TEST
	s.s
	x.i
EndStructure

Procedure CreateObject(x)
	Protected *Res.TEST = AllocateStructure(TEST)
	*Res\s    = Str(x)
	*Res\x    = x
	ProcedureReturn *Res
EndProcedure

*x1.TEST = CreateObject(1)
*x2.TEST = CreateObject(2)
*x3.TEST = CreateObject(3)
*x4.TEST = CreateObject(4)

Debug *x1\s
Debug *x1\x
Debug *x2\s
Debug *x2\x
Debug *x3\s
Debug *x3\x
Debug *x4\s
Debug *x4\x

FreeStructure(*x1)
FreeStructure(*x2)
FreeStructure(*x3)
FreeStructure(*x4)
Post Reply