Can a procedure return multiple values?[CLOSE]

Just starting out? Need help? Post your questions and find answers here.
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

Can a procedure return multiple values?[CLOSE]

Post by SkyManager »

Please correct me if I am wrong.
PB procedure can not return array or structure
For example, I need a procedure to return to me a point or a location which are having at least two variables (position X and position Y). Sometimes, I need more such as Color, or 3-dimension vector value.

My question is :
Is there any alternative for a procedure to return multiple values :?:
Last edited by SkyManager on Sun Mar 11, 2007 3:57 am, edited 1 time in total.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Your best bet is to pass a pointer to a structure as one of your procedure parameters and then have the procedure modify the fields of the structure.

Code: Select all

Structure test
  a.l
  b.l
EndStructure

Procedure ModifyStruc(*ts.test)
  *ts\a=100
EndProcedure

;Dummy data.
x.test
  x\a = 1
Debug x\a

;Pass to the procedure.
ModifyStruc(@x)  ;@ is the 'address of...' operator.
Debug x\a  ;See, the value has changed.
Alternative methods include having the procedure returning a pointer to a newly allocated block of memory etc. However, the calling program then has to take steps to free the memory etc.
I may look like a mule, but I'm not a complete ass.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Heres a simple way that multiple values can be returned by a procedure:

Code: Select all

Procedure.l MakePoints(Value.l)
    Static Coords.POINT
    Coords\x = Value & $FFFF
    Coords\y = (Value >> 16) & $FFFF
    ProcedureReturn @Coords
EndProcedure

*Screen.POINT = MakePoints(67109632)

Debug *Screen\x
Debug *Screen\y
Here you are returning a single 32bit long which is an address in memory to the data you need. You then use a structured pointer to access that data.

Read this:
http://www.purebasic.fr/english/viewtopic.php?t=10753
--Kale

Image
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

Here's another way of returning multiple values:

Code: Select all

; Return Multiple Results from Procedure  AKJ  02-Mar-07

Procedure.d CircleInfo(radius.d, *area, *circum)
; Return the area and circumference for a circle of given radius
Protected area.d, circ.d
area = #PI*radius*radius: PokeD(*area, area)
circ = 2*#PI*radius: PokeD(*circum, circ)
EndProcedure

; Main program
Define a.d, c.d ; Area and circumference
CircleInfo(5.0, @a, @c)
Debug "Area = "+StrD(a)+"   Circumference = "+StrD(c)
End
Anthony Jordan
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

Post by SkyManager »

Hi, Kale,

In your sample code, you use a structure POINT. Do I need to create the POINT structure using the Declare statement before the MakePoints method?

Code: Select all

Procedure.l MakePoints(Value.l)
    Static Coords.POINT
    Coords\x = Value & $FFFF
    Coords\y = (Value >> 16) & $FFFF
    ProcedureReturn @Coords
EndProcedure

*Screen.POINT = MakePoints(67109632)

Debug *Screen\x
Debug *Screen\y 
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

SkyManager,

POINT is a built in structure.

You can see its definition in the Structure Viewer, available from the Tools menu of the IDE.

btw. Welcome to PureBasic. Keep the questions coming.
Post Reply