Procedures that can return structures

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
The seperator
User
User
Posts: 17
Joined: Sun Apr 25, 2004 9:12 pm
Location: Europe, Belgium

Procedures that can return structures

Post by The seperator »

I allready know you can solve this problem by using pointers but you
still have to use freememory() each time to save memory.
VPureBasic
User
User
Posts: 59
Joined: Fri Apr 25, 2003 6:09 pm
Location: Quebec - Canada

Post by VPureBasic »

Hi The Separator,

You wrote:
I allready know you can solve this problem by using pointers but you
still have to use freememory() each time to save memory
Maybe You can do it like this too:

Code: Select all

Structure STRUCT
     Argument_01.f
     Argument_02.f
EndStructure


Procedure.l    MathFormula( x.f, y.f )

     DefType.STRUCT ReturnStruct
     
     ; Your Code
     ReturnStruct\Argument_01 = x * y
     ReturnStruct\Argument_02 = y / x
     
     ProcedureReturn @ReturnStruct
     
EndProcedure


*Result.STRUCT = MathFormula( 5.0, 10.2 )

ABC.f = *Result\Argument_01
CBA.f = *Result\Argument_02
Hope this will help

Roger
Last edited by VPureBasic on Wed Jun 02, 2004 3:52 am, edited 1 time in total.
Everything is possible with PureBASIC... All you're missing is imagination!
Doobrey
Enthusiast
Enthusiast
Posts: 218
Joined: Sat Apr 26, 2003 4:47 am
Location: Dullsville..population: me
Contact:

Post by Doobrey »

It`s better to create the structure outside of a procedure, as PB will clean up after itself when the procedure ends, and any variables or structures created within it go off to meet their maker..

Code: Select all

Structure STRUCT
     Argument_01.f
     Argument_02.f
EndStructure


Procedure.l    MathFormula( x.f, y.f ,*ReturnStruct.STRUCT)

     ; Your Code
     ReturnStruct\Argument_01 = x * y
     ReturnStruct\Argument_02 = y / x
     
    ;## You could return a value here too, indicating success of failure.
     
EndProcedure

  Result.STRUCT
  MathFormula( 5.0, 10.2,@Result )

ABC.f = Result\Argument_01
CBA.f = Result\Argument_02
VPureBasic
User
User
Posts: 59
Joined: Fri Apr 25, 2003 6:09 pm
Location: Quebec - Canada

Post by VPureBasic »

Hi Doobrey,

You write:
It`s better to create the structure outside of a procedure...
Your are right!

Do not takes did badly... Your code looks good... but "The Separator" asked for:
Procedure that will return a structure... :wink:


Roger
Everything is possible with PureBASIC... All you're missing is imagination!
The seperator
User
User
Posts: 17
Joined: Sun Apr 25, 2004 9:12 pm
Location: Europe, Belgium

Post by The seperator »

Indeed what i mean is you can write something like:

Code: Select all

Structure STRUCT 
     Argument_01.f 
     Argument_02.f 
EndStructure 


Procedure.STRUCT MathFormula( x.f, y.f ) 

     DefType.STRUCT ReturnStruct 
      
     ; Your Code 
     ReturnStruct\Argument_01 = x * y 
     ReturnStruct\Argument_02 = y / x 
      
     ProcedureReturn ReturnStruct 
      
EndProcedure 


Result.STRUCT = MathFormula( 5.0, 10.2 ) 

ABC.f = Result\Argument_01 
CBA.f = Result\Argument_02 
It's just that in some cases it would be easier to directly return user defiened types.
POedBoy
New User
New User
Posts: 8
Joined: Tue Jun 01, 2004 9:31 pm
Location: La Verne, CA, USA

Post by POedBoy »

This would be *very* nice.
VPureBasic
User
User
Posts: 59
Joined: Fri Apr 25, 2003 6:09 pm
Location: Quebec - Canada

Post by VPureBasic »

Hi The Separator,

The only way that you can do it in PB as your looking for is the way Doobrey write his code... PB can't return more than 1 value for answer. That's why you should add your answer address structure as a parameter.

This should looks like this:

Code: Select all

Structure STRUCT 
     Argument_01.f 
     Argument_02.f 
EndStructure 

Global Result.STRUCT

Procedure MathFormula( x.f, y.f, *ReturnStruct.STRUCT ) 
     ; Your Code 
     *ReturnStruct\Argument_01 = x * y 
     *ReturnStruct\Argument_02 = y / x 
     ;      
EndProcedure 


MathFormula( 5.0, 10.2, @Result ) 

ABC.f = Result\Argument_01 
CBA.f = Result\Argument_02 
 
I hope this will help...

Roger
Everything is possible with PureBASIC... All you're missing is imagination!
The seperator
User
User
Posts: 17
Joined: Sun Apr 25, 2004 9:12 pm
Location: Europe, Belgium

Post by The seperator »

I allready know this, It's just a request for future releases. :)
oldefoxx
Enthusiast
Enthusiast
Posts: 532
Joined: Fri Jul 25, 2003 11:24 pm

Post by oldefoxx »

The general rule for procedures in every language is that you can pass a large number of parameters to a procedure, which are placed on the stack by the compiler before calling the procedure, but you can only return one value when you exit the procedure, which is normally in the AX register for 16-bit values, or the combined DX,AX registers when returning 32 bit values. The compiler then performs whatever action is required with the returned value.

So every returned type has to be limited to 16 or 32 bit values. Meaning structures and strings are returned as pointers. The compiler has to remove all the passed parameters from the stack when the procedure is existed, because failure to do this will cause the stack to grow and grow until its capacity is exceeded. I've been told that the C language leaves it to the coder to remember to do this, which would be a royal pain.

Point is, to change the manner in which PureBasic returns information from a procedure is likely to make it unsuitable for creating libraries that can be called from other languages. It's taken many years to work out the bugs of having one language take advantage of another for specific needs, and here you are proposing to make PureBasic incompatable with a standard that has given us that ability.
has-been wanna-be (You may not agree with what I say, but it will make you think).
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post by Froggerprogger »

don't forget the st0-register for floats.
sorry I'm drunken and just wanny type some stuff 8)

... to say something- nonnonnonnonsense: PB could translate during compilation something like:

Code: Select all

Procedure.MYSTRUCT myproc()
  ProcedureReturn bla.MYSTRUCT
EndProcedure
a = myproc
to a consistent code such as:

Code: Select all

Procedure.l myproc()
  bla = AllocateMemory(SizeOf(MYSTRUCT))
  ProcedureReturn bla
EndProcedure
a = myproc
So what I mean is, PB could do the trick for us.

(Though it would be a little bit strange to allocate global memory for the result-structure, because it had to be protected on the first look, but it had to be global to keep it's elements after procedure's end. Hmmm. I still prefer the common and clear "structure-pointer as parameter"-version.)
%1>>1+1*1/1-1!1|1&1<<$1=1
Post Reply