functions returning blitzarrays

Blitz3D Forums/Blitz3D Beginners Area/functions returning blitzarrays

koekjesbaby(Posted 2003) [#1]
hi,

can somebody tell me how to return a blitzarray with a function? declaring a function like this:

Function func[10](x,y,z)


does not work. and using a type to return a blitzarray like this:

Type something
	Field array[10] 
End Type

Function func.something(x,y,z)
	Local dummy.something = New something
	dummy\array[0]=x+y+z

	Return dummy
End Function


does work, but keeps filling your memory with new something types (eventho you declare them as local so they should be forgotten when the code leaves the function (or do i make a mistake here?)).

thanks,
hessel


GfK(Posted 2003) [#2]
Arrays are global. You don't need to pass them back and forth between functions.

Type collections are global too.


koekjesbaby(Posted 2003) [#3]
eh, oops, good point. i got kinda confused by an tutorial on blizcoder.com.

to double check, you mean something like this:
Function func(dummy.something, x)
	dummy\array[1] = x
End Function

testType.something = New something
func(testType,3) ;this changes the second element of the array of testType to 3


and: thank you!


Curtastic(Posted 2003) [#4]
blitz arrays are not global unless you say they are

you can pass the blitz array instead of returning it like:

local nums[5]
clear nums
;sets the array to 1s ?!
Function clear(a[5])
	For i=0 To 5
		a[i]=1
	Next
End Function



Oldefoxx(Posted 2003) [#5]
Here is a direct quote from the Help file:
An array may be dimensioned at more than one point in a program, each time an array is dimensioned, it's previous contents are discarded. Arrays may be dimensioned inside functions, but a corresponding 'Dim' statement of the same array must also appear somewhere in the main program. For example:

Dim test(0,0) 
Function Setup( x,y ) 
Dim test(x,y) 
End Function 


That suggests that arrays are indeed global. If you have experienced something somewhat different, it may be an undocumented behavour, it may result in some unexpected byproduct, and it may not behave that way in a future release. I do note, however, that though you did use the leyword "Local", that your array was dimensioned outside the function (in the main program), and that as a consequence, you could still pass a reference to it - but that would have been entirely unnecessary had it been declared as "Global". However, it might be handy if you every write a generic process that you want to be able to use selectively with any given array at any given time.


Koriolis(Posted 2003) [#6]
Didn't you notice that the array in the previous example uses '[]' and noft '()'. These kind of arays are completly different, and are ofter refered under the (confusing) name of "blitz arrays". They can be local unlike usual arrays, they can be in types, etc.


cyberseth(Posted 2003) [#7]
I tend to think of Blitz arrays as basically just X number of standard variables, rather than an array. I can't quite explain the difference in the way that I see it.. but anyhow... because of this you can't really seem to define a function's type as more than one variable. Therefore you can't define it as a Blitz array type, coz a Blitz array is just X amount of variables.

HOWEVER, you could define it as a type, and have the type contain the variables you need. Because types are global and they also have pointers.

Type vartype
    Field barray[100]
End Type

v.vartype = new vartype
DoStuff v

Function DoStuff(v.vartype)
    For i=0 to 100
        v\barray[i] = Rand(100)
    Next
End Function


This is the best example of "ByRef" parameters. An array where you change variables' information inside the array without having to send the value back out. (Because you can only send back ONE value.)


roboslug(Posted 2003) [#8]
But he doesn't need to do anything except pass the array to the function and modify the contents inside...why are half of you feeding him types?

BlitzArrays are explained at www.blitzcoder.com in the code archives->undocumented blitz commands (or something like that).

Death to the dim()! - hey...that may be my Blitz3d sig. :-)


koekjesbaby(Posted 2003) [#9]
i got my solution after Gfk's post, and it is exaclty what cyberseth typed (and what i wanted to type in my second post). but thanks for the helpful gesture!