Procedures that can return structures
-
- User
- Posts: 17
- Joined: Sun Apr 25, 2004 9:12 pm
- Location: Europe, Belgium
Procedures that can return structures
I allready know you can solve this problem by using pointers but you
still have to use freememory() each time to save memory.
still have to use freememory() each time to save memory.
-
- User
- Posts: 59
- Joined: Fri Apr 25, 2003 6:09 pm
- Location: Quebec - Canada
Hi The Separator,
You wrote:
Hope this will help
Roger
You wrote:
Maybe You can do it like this too:I allready know you can solve this problem by using pointers but you
still have to use freememory() each time to save memory
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
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!
-
- Enthusiast
- Posts: 218
- Joined: Sat Apr 26, 2003 4:47 am
- Location: Dullsville..population: me
- Contact:
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
-
- User
- Posts: 59
- Joined: Fri Apr 25, 2003 6:09 pm
- Location: Quebec - Canada
Hi Doobrey,
You write:
Do not takes did badly... Your code looks good... but "The Separator" asked for:
Roger
You write:
Your are right!It`s better to create the structure outside of a procedure...
Do not takes did badly... Your code looks good... but "The Separator" asked for:
Procedure that will return a structure...![]()
Roger
Everything is possible with PureBASIC... All you're missing is imagination!
-
- User
- Posts: 17
- Joined: Sun Apr 25, 2004 9:12 pm
- Location: Europe, Belgium
Indeed what i mean is you can write something like:
It's just that in some cases it would be easier to directly return user defiened types.
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
-
- User
- Posts: 59
- Joined: Fri Apr 25, 2003 6:09 pm
- Location: Quebec - Canada
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:
I hope this will help...
Roger
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
Roger
Everything is possible with PureBASIC... All you're missing is imagination!
-
- User
- Posts: 17
- Joined: Sun Apr 25, 2004 9:12 pm
- Location: Europe, Belgium
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.
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).
-
- Enthusiast
- Posts: 423
- Joined: Fri Apr 25, 2003 5:22 pm
- Contact:
don't forget the st0-register for floats.
sorry I'm drunken and just wanny type some stuff
... to say something- nonnonnonnonsense: PB could translate during compilation something like:
to a consistent code such as:
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.)
sorry I'm drunken and just wanny type some stuff

... to say something- nonnonnonnonsense: PB could translate during compilation something like:
Code: Select all
Procedure.MYSTRUCT myproc()
ProcedureReturn bla.MYSTRUCT
EndProcedure
a = myproc
Code: Select all
Procedure.l myproc()
bla = AllocateMemory(SizeOf(MYSTRUCT))
ProcedureReturn bla
EndProcedure
a = myproc
(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