Page 1 of 2
ProcedureReturn Limitation
Posted: Sat Aug 11, 2007 7:59 am
by electrochrisso
According to docs ProcedureReturn only passes one variable at a time, but I want to pass more variables back, as in the simple example below.
Code: Select all
Procedure Testing(a,b,c)
Static a,b,c
d=a*5
e=b+100
f=c/4
ProcedureReturn d,e,f
EndProcedure
How come PureBasic was not designed to return more than 1.
I can get around the problem using other methods but it gets a bit messy using the other methods I have thought of.
Has anyone got ideas of simple ways of simulating this.
Thankyou
Posted: Sat Aug 11, 2007 8:37 am
by dell_jockey
you could return a structure as a single entity. This structure would contain your set of return variables.
Re: ProcedureReturn Limitation
Posted: Sat Aug 11, 2007 8:47 am
by PB
> How come PureBasic was not designed to return more than 1
Do any other languages return more than one? (I'm naive; not being sarcastic).
But you could always make your d,e,f variables Global and just not return them,
but just get their values after the procedure has finished. That's what I do.
Posted: Sat Aug 11, 2007 9:05 am
by gnozal
Quick example using a structure :
Code: Select all
Structure Testing
d.l
e.l
f.l
EndStructure
Procedure.l Testing(a, b, c)
Static TestingResult.Testing
TestingResult\d = a * 5
TestingResult\e = b + 100
TestingResult\f = c / 4
ProcedureReturn @TestingResult
EndProcedure
*TestingResult.Testing = Testing(1, 2, 3)
Debug *TestingResult\d
Debug *TestingResult\e
Debug *TestingResult\f
Posted: Sat Aug 11, 2007 9:22 am
by Joakim Christiansen
Yeah, that's the proper way gnozal.
Posted: Sat Aug 11, 2007 10:07 am
by r_hyde
PB wrote:Do any other languages return more than one?
Not any compiled ones I'm aware of, but Python for instance can return multiple values in a single return statement (
return d, e, f)
Posted: Sat Aug 11, 2007 10:59 am
by Kaeru Gaman
the question is.. how would you interprete a multiple return value?
away from the difficulties how to implement it technically,
how should code be written then?
a return-value CAN only be one value,
because on the left site of an assignment is always only one identifier.
I'm curious how Python would handle this.. do you have an example?
Posted: Sat Aug 11, 2007 11:43 am
by traumatic
Kaeru Gaman wrote:I'm curious how Python would handle this.. do you have an example?
Exactly as you already posted:
Posted: Sat Aug 11, 2007 11:58 am
by Kaeru Gaman
hm... ok
but this is not conform to some basic rules, neither in BASIC, nor in C, nor in others.
maybe in COBOL it was possible, can't remember...
it would break the assignment rule "one on the left",
even WinAPI follows this rule.
and it's not necessary, like shown by gnozal's example.
Posted: Sat Aug 11, 2007 12:23 pm
by r_hyde
I wasn't suggesting it should be in PB, and I think it might not be possible in a compiled language (registers and whatnot). You're correct to say that it's not necessary in PB (or C/C++, etc) because we have pointers and structures. Python doesn't have pointers, though, so there it comes in very handy

Posted: Sat Aug 11, 2007 1:32 pm
by Kale
Kaeru Gaman wrote:but this is not conform to some basic rules, neither in BASIC, nor in C, nor in others.
Exactly! and that's why Python is a different language.

There are some very cool features in Python that don't occur in any other language. You should check it out.

Posted: Sat Aug 11, 2007 1:43 pm
by Kaeru Gaman
Kale wrote:Kaeru Gaman wrote:but this is not conform to some basic rules, neither in BASIC, nor in C, nor in others.
Exactly! and that's why Python is a different language.

There are some very cool features in Python that don't occur in any other language. You should check it out.

I don't doubt, and sure I'll have to have a closer look at it.

Posted: Sat Aug 11, 2007 2:00 pm
by SoulReaper
Thankyou
I always wondered how to pass back more then one parameter
Still Learning
Regards
Kevin

Posted: Sat Aug 11, 2007 2:13 pm
by Kaeru Gaman
gnozals solution has one little weakness:
the return-struct is static, so it will stay inside the procedure.
he is just assigning a structured pointer to it.
if the proc is called twice, the old value will be overwritten.
Code: Select all
Structure Testing
d.l
e.l
f.l
EndStructure
Procedure.l Testing(a, b, c)
Static TestingResult.Testing
TestingResult\d = a * 5
TestingResult\e = b + 100
TestingResult\f = c / 4
ProcedureReturn @TestingResult
EndProcedure
*TestingResult1.Testing = Testing(1, 2, 3)
Debug *TestingResult1\d
Debug *TestingResult1\e
Debug *TestingResult1\f
*TestingResult2.Testing = Testing(4, 5, 6)
Debug "---"
Debug *TestingResult2\d
Debug *TestingResult2\e
Debug *TestingResult2\f
Debug "---"
Debug *TestingResult1\d
Debug *TestingResult1\e
Debug *TestingResult1\f
for WinAPI calls that need a structured return,
you create your structured variable before the call and pass it's pointer to the routine.
you can chose the same approach for your own procedures.
Code: Select all
Structure Testing
d.l
e.l
f.l
EndStructure
Procedure.l Testing(a, b, c, *T.Testing)
*T\d = a * 5
*T\e = b + 100
*T\f = c / 4
ProcedureReturn #True
EndProcedure
TestingResult1.Testing
Testing(1, 2, 3, @TestingResult1)
Debug TestingResult1\d
Debug TestingResult1\e
Debug TestingResult1\f
Debug "---"
TestingResult2.Testing
Testing(4, 5, 6, @TestingResult2)
Debug TestingResult2\d
Debug TestingResult2\e
Debug TestingResult2\f
Debug "---"
Debug TestingResult1\d
Debug TestingResult1\e
Debug TestingResult1\f
Posted: Sat Aug 11, 2007 2:32 pm
by SoulReaper
@Kaeru Gaman
Great Example and I understood it
