Sorry if this has been asked before but I am stumped!How do I pass a type to a function so I can do manipulation on it.
TYPE ball
x_pos
y_pos
colour
ENDTYPE
local balls[] AS ball
How would I pass this to a function so I could DIMPUSH a new instance of ball to the balls[] instance array.
Thanks in advance for any help.
You do something like :
LOCAL balls[] as ball
ADD(balls[])
FUNCTION Add%:some[] as ball
or you can
so you still have to give the parameter in the function the AS ball extension.This does not make the function very reuseable as you have to give it the type name.
I'm not quite sure what your 2nd comment means.
But here's a quick example that shows how to pass TYPEs:
TYPE TBall
x_pos
y_pos
colour%
ENDTYPE
GLOBAL balls[] AS TBall
FUNCTION AddBall: ball AS TBall
DIMPUSH balls[], ball
ENDFUNCTION
Additionally, you can use functions in TYPEs:
TYPE TBall
x_pos
y_pos
colour%
FUNCTION Set: x, y, colour%
self.x_pos = x
self.y_pos = y
self.colour = colour
ENDFUNCTION
FUNCTION Draw:
DRAWSPRITE 1, self.x, self.y
ENDFUNCTION
ENDTYPE
GLOBAL balls[] AS TBall
GLOBAL ball AS TBall
ball.Set(10, 20, RGB(255,0,0))
DIMPUSH balls[], ball
ball.Draw()
Or, a function that returns a TYPE:
FUNCTION FindBallByColour AS TBall: colour%
FOREACH ball IN TBalls[]
IF ball.colour = colour THEN RETURN ball
NEXT
ENDFUNCTION
Are you asking if you have to specify the TYPE in the FUNCTION declaration?
And, wonder that if you didn't, you would be able to use the function for multiple purposes / TYPEs?
Then, no, that's not possible. Each FUNCTION has to specify exactly what it can receive, and exactly what it can send back. The exception is some parameters can be optional by just including a default value such as:
// The last two parameters are optional
FUNCTION AddNumbers%: v1%, v2%, v3%=-1, v4%=-1
LOCAL result%
result = v1 + v2
IF v3 <> -1 THEN INC result, v3
IF v4 <> -1 THEN INC result, v4
ENDFUNCTION
Ha, bad example, you can't add '-1'!
When using the optional parameters, they must be the right most parameters.
HOWEVER, there is no way of passing a TYPE instance optionally!
I needed that a few times and had to create a dummy global instance to pass in these cases.
If this is what you are asking (not the 'optional' stuff), can you give me an example of where you would want this?
I was going to mention about passing a single item from the array, but then re-read the original post, which wasn't what was wanted... :)
QuoteThis does not make the function very reuseable as you have to give it the type name.
I presume you want something like C's void parameter - in which case you would have to go INLINE and be very careful - passing parameters as known items helps to prevent dodgy coding!
Saying that, there are times (usually for very quick jumping or memory allocation) where the possibility of a known type would render the function useless (or would make it inefficient).
Alternatively, if you are looking for re-usability, I would go with an extended type and if needed setup multiple functions to do what you want.