Any var-type return for Functions

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Any var-type return for Functions

Post by Psychophanta »

Currently, procedures can return only PB internal native variable types, but it is more and more necessary to allow to return any variable type.
Sorry if this suggest was post in the past, but i don't find. :)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Dräc
Enthusiast
Enthusiast
Posts: 150
Joined: Sat Oct 09, 2004 12:10 am
Location: Toulouse (France)
Contact:

Post by Dräc »

[refresh :)]
Is using structure has ProcedureReturn planned for an immediate future of the PB version? (and available for Interface also...)
Dräc
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

no.
Structures are always passed byRef.
that means you pass the pointer of a structure to be filled to the Procedure.
the Return Value can also be a pointer to a structure.

the Return Value of a Procedure is at max a Quad.
sure you can put this Quad into a StructureUnion.

Code: Select all

Structure DoubleCoord
  C1.COORD
  C2.COORD
EndStructure

Structure Passer
  StructureUnion
    StrP.DoubleCoord
    QuaP.q
  EndStructureUnion 
EndStructure

Procedure.q GetCoords( X.w, Y.w )
  Protected OutCoords.Passer        ; define an internal Passer
  OutCoords\StrP\C1\x = X + 100       ; fill the structured Part
  OutCoords\StrP\C1\y = Y - 100
  OutCoords\StrP\C2\x = X - 100
  OutCoords\StrP\C2\y = Y + 100
  ProcedureReturn OutCoords\QuaP    ; return the quad Part
EndProcedure

Define TestCoords.Passer

TestCoords\QuaP = GetCoords( 200, 500 )         ; fetch a quad

Debug "C1\x = " + Str( TestCoords\StrP\C1\x )   ; debug the structured content
Debug "C1\y = " + Str( TestCoords\StrP\C1\y )
Debug "C2\x = " + Str( TestCoords\StrP\C2\x )
Debug "C2\y = " + Str( TestCoords\StrP\C2\y )
but this is rather unusual, and only possible for structures that cover maximum 64bit.
oh... and have a nice day.
Dräc
Enthusiast
Enthusiast
Posts: 150
Joined: Sat Oct 09, 2004 12:10 am
Location: Toulouse (France)
Contact:

Post by Dräc »

Oh! I was’nt aware that you are on the PB development team, Kaeru Gaman :mrgreen: :twisted: ;)
Dräc
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Note though that if you want to ProcedureReturn a pointer to a structure and have all members of the structure available to the caller you must use AllocateMemory or a persistent scope like global or the memory is freed on exit.
BERESHEIT
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

Dräc wrote:Oh! I was’nt aware that you are on the PB development team, Kaeru Gaman :mrgreen: :twisted: ;)
:lol:
..no need to be on the team to answer this, because we just had a discussion about this lately,
and Freak himself stated that it is not at all planned to change the returning from how it is now.

the ReturnValue is in EAX for Long or smaller, for Quad or Double there is one additional Register used.
Freak said, they are not thinking about changing this way.
oh... and have a nice day.
Dräc
Enthusiast
Enthusiast
Posts: 150
Joined: Sat Oct 09, 2004 12:10 am
Location: Toulouse (France)
Contact:

Post by Dräc »

:D Ok! Thx Kaeru Gaman!
Dräc
Post Reply