ProcedureReturn Limitation

Everything else that doesn't fall into one of the other PB categories.
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

ProcedureReturn Limitation

Post 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
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

you could return a structure as a single entity. This structure would contain your set of return variables.
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: ProcedureReturn Limitation

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post 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
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

Yeah, that's the proper way gnozal.
I like logic, hence I dislike humans but love computers.
r_hyde
Enthusiast
Enthusiast
Posts: 155
Joined: Wed Jul 05, 2006 12:40 am

Post 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)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

the question is.. how would you interprete a multiple return value?

Code: Select all

d, e, f = Testing(a,b,c)
:?:

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?
oh... and have a nice day.
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

Kaeru Gaman wrote:I'm curious how Python would handle this.. do you have an example?
Exactly as you already posted:

Code: Select all

var1, var2, var3 = function()
Good programmers don't comment their code. It was hard to write, should be hard to read.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
r_hyde
Enthusiast
Enthusiast
Posts: 155
Joined: Wed Jul 05, 2006 12:40 am

Post 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 :)
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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. :wink: There are some very cool features in Python that don't occur in any other language. You should check it out. :)
--Kale

Image
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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. :wink: 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. ;)
oh... and have a nice day.
SoulReaper
Enthusiast
Enthusiast
Posts: 372
Joined: Sun Apr 03, 2005 2:14 am
Location: England

Post by SoulReaper »

Thankyou :)

I always wondered how to pass back more then one parameter :wink:
Still Learning :lol:

Regards
Kevin :wink:
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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
oh... and have a nice day.
SoulReaper
Enthusiast
Enthusiast
Posts: 372
Joined: Sun Apr 03, 2005 2:14 am
Location: England

Post by SoulReaper »

@Kaeru Gaman
Great Example and I understood it :lol:
Post Reply