News:

Function Finder  Find all the functions within source code files

Main Menu

Returning Types (UDTs) from functions

Started by MudbuG, September 02, 2008, 10:19:43 PM

Previous topic - Next topic

MudbuG

I would love to be able to return types from functions.  I searched the board and didn't find anything on this topic (could be my poor search skills).  I tried a few different things and I came up with what seems to be a workable solution.
It seems to work, but I am not really sure if it is doing what I think it is doing.  Does anyone know?  Here is the code (Yeah, it looks a little strange):


; PROJECT : Project4
; AUTHOR  : Damon Ellerbee
; CREATED : 9/2/2008
; ---------------------------------------------------------------------

type TThing
  Num
endtype

Dim AllThings as TThing List
Dim Thing1 as TThing
Dim Thing2 as TThing

Thing1 = CreateThing(1)
Thing2 = CreateThing(2)

Print Thing1.Num
Print Thing2.Num
WaitKey

Function CreateThing(Num)
  AllThings = New TThing
  AllThings.Num = Num
EndFunction AllThings


Thanks!
-MudbuG

kevin

#1
  It appears to work,  not entirely sure that's safe.

This does the same thing.

PlayBASIC Code: [Select]
   type TThing
Num
endtype

; Dim AllThings as TThing List
Dim Thing1 as TThing
Dim Thing2 as TThing

CreateThing(Thing1(),1)
CreateThing(Thing2(),2)

Print Thing1.Num
Print Thing2.Num
WaitKey


Function CreateThing(me.tthing,num)
me = New TThing
me.Num = Num
EndFunction



 You can return pointers to typed structures..  While this will work it's unsafe, the function is creating a instance every time, but the memory is not being released.  

PlayBASIC Code: [Select]
   Type tThing
Num
EndType

; Dim AllThings as TThing List
Dim Thing1 as TThing
Dim Thing2 as TThing

; note the explicit type declaration.
Thing1.Tthing=CreateThing(1)
Thing2.Tthing=CreateThing(2)

Print Thing1.Num
Print Thing2.Num
WaitKey


Function CreateThing(num)
me = New TThing
me.Num = Num
EndFunction me as tthing pointer



  This is safe (no leaks) version of the previous approach.  It uses a 'temp' global type to store the newly created structure in.  

PlayBASIC Code: [Select]
   Type tThing
Num
EndType

; Dim AllThings as TThing List
Dim SafeNewThing as tthing

Dim Thing1 as TThing
Dim Thing2 as TThing

; note the explicit type declaration.
For lp=0 to 1000
Thing1.Tthing=CreateThing(1)
Thing2.Tthing=CreateThing(2)
next

Print Thing1.Num
Print Thing2.Num

WaitKey


Function CreateThing(num)
SafeNewThing= new tthing
me=GetLIstPtr(safeNewthing())
me.Num = Num
EndFunction me as tthing pointer





 MODEDIT:


   This was added years and years ago, although from what i can tell nobody has ever used it..  To a return a USER DEFINED TYPE FROM A FUNCTION or PROTECTED SUB ROUTINE, just declare it like so.

     So this would return Result  structure to the caller.    Make sure you Initialize result within the function though otherwise it'll return NULL.

PlayBASIC Code: [Select]
   ; delcare a Type so we have something to pass/return from functions
Type Vector2D
x#,y#
EndType

// delcare a list of type Vector2D
Dim VL as Vector2D list


// create a bunch of 2d vectors and store them in the list
for lp =0 to 10
VL = CreateVector(lp*20,rnd(100))
next

// display whater is in our vector list
for each VL()
print " X:"+str$(VL.X)+ " Y:"+str$(VL.y)
next

// show screeen and wait for a key press
Sync
waitkey
end



Function CreateVector(X#,Y#)
result = new Vector2D
result.x=x#
result.y=y#
EndFunction Result as Vector2D





     One thing to note is that make sure your writing the returned type handle into a type array or list..  otherwise you'll end up leaking memory.


thaaks

I still find type handling a bit complicated - I always have to mess around to find out how the notation looks like if I want to pass a type instance into a function.

The most understandable for me is Kevin's first code example except the array brackets in the CreateThing call.

CreateThing(Thing1(),1)
CreateThing(Thing2(),2)


It looks like passing an array into the function although I'm only passing one instance.

So I would say more readable would be either

CreateThing(Thing1,1)
CreateThing(Thing2,2)

or

CreateThing(Thing1.TThing,1)
CreateThing(Thing2.TThing,2)

to make the type information more visible.

Kevin, do you plan to fix the memory leaks and maybe change or improve notation?
And do you also plan to add some other Type related functionalities like (fixed size) arrays of types inside types and INC working for type fields?

Cheers,
Tommy