Page 1 of 1

More than one (two?) procedure return value

Posted: Sun Jun 05, 2022 7:43 pm
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?

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

Posted: Sun Jun 05, 2022 8:12 pm
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 @

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

Posted: Sun Jun 05, 2022 8:45 pm
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.

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

Posted: Sun Jun 05, 2022 9:46 pm
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.

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

Posted: Sun Jun 05, 2022 9:55 pm
by Cezary
Yes, but sometimes it's worth doing it differently than everyone else. PureBasic is a good example of this.

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

Posted: Mon Jun 06, 2022 2:27 pm
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.

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

Posted: Mon Jun 06, 2022 2:52 pm
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.

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

Posted: Mon Jun 06, 2022 5:22 pm
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)