Two fdunctions or use some globals?

Blitz3D Forums/Blitz3D Programming/Two fdunctions or use some globals?

_PJ_(Posted 2009) [#1]
Just a general question, really, what do you all consider to be the "best" method (I appreciate, it's largely down to circumstance, but all comments welcome!) when dealing with function returns where you may want more than one particular piece of information.

For a typical example, returning coordinates.

One way would be to use three functions,
Function GetX(Parameter)
Return Blah
End Function
Function GetY(Parameter)
Return Blah
End Function
Function GetZ(Parameter)
Return Blah
End Function


Another approach, maybe to just use a generic function and stick the values you want somewhere nice and safe, such as a Type or Global variable etc:

Function GetXYZ(Parameter)
NewCoords.Coords = New Coords
NewCoords.X=Blah
NewCoords.Y=Blah
NewCoords.Z=Blah
End Function
[/code]

Admittedly, the 'coordinate' example may be misleading thanks to built in functions like EntityX() etc. but it's the principle I'm really concerned with.

Looking at storing in Types/Variables, would of course mean that the data can be rertrieved faster for later use, but may be outdated, also, with Types, it would maybe need to be cleared before making a new one, else you'd end up with a bunch of obsolete / duplicate records eating up memory, then again, Types offer the opportunity for "Undo" or storage for replay.


Guy Fawkes(Posted 2009) [#2]
id use

Type Coords
Field X#
Field Y#
Field Z#
End Type

NewCoords.Coords = New Coords
NewCoords.X#=Blah
NewCoords.Y#=Blah
NewCoords.Z#=Blah

Function GetXYZ(Parameter)
For FinalCoords = Each Coords
FinalCoords.X# = Blah
FinalCoords.Y# = Blah
FinalCoords.Z# = Blah
Next
Return FinalCoords
End Function


best i can do :)

~DS~


gosse(Posted 2009) [#3]
Use a container structure such as a Type for return values where you have more than 1 data type to return.


Ross C(Posted 2009) [#4]
I like to just use the seperate functions. Type seems like too much hard work :P And there's more chance i'll make an ass of it.


_PJ_(Posted 2009) [#5]
Hey DSW, that's a good point, I didn't even consider actually returning the specific instance for the Types :D


Guy Fawkes(Posted 2009) [#6]
hehe thanks :)

i learn from the best :P


Stevie G(Posted 2009) [#7]

hehe thanks :)

i learn from the best :P



Your code doesn't work so perhaps you need a new tutor :)

If there are only a couple of bits of info I need I tend to use some global vars for simplicity. IMO it's overkill to use a global type unless you have alot of information which you need returned.

Exactly what do you need returned from the function?

Stevie


Guy Fawkes(Posted 2009) [#8]
it doesnt matter. it still gave him an idea.

so there. /rasp

LOL XD

~DS~


_PJ_(Posted 2009) [#9]
Exactly what do you need returned from the function?


Oh it wasn't any specific scenario, although the thought occurred to me when I was working with mouse coordinates it isn't especially relevant. I just thought to myself, is it really necessary to have two functions one each for X and Y, so I thought I'd throw the question out along with whatever alternatives I could think of, in case someone had some remarkably novel technique :)

I agree Types would be overkill much moreso than global variables for just a couple of instances of data. Overall it's definitely very situation-dependant.


Guy Fawkes(Posted 2009) [#10]
yea. i guess.

i use types cuz theyre easy lol

:P

~DS~