Blitz array as argument?

Blitz3D Forums/Blitz3D Beginners Area/Blitz array as argument?

NewtSoup(Posted 2003) [#1]
Hi,
Can a blitz array be passed as an argument?

eg.
local my_data$[2]
my_data[0]="Fred"
my_data[1]="Wilma"
my_data[2]="Barney"

add_surname( my_data )

Function add_surname( b_arr )
b_arr[0]=b_arr[0]+" Flintstone"
....etc
End Function

Obviously the above does not work but it gets across the idea of what I want to do.

As I type I hear cries of object and handle in the distance but what the hell, I'm feeling lazy so I'm going to ask..

- Luke


semar(Posted 2003) [#2]
Can a blitz array be passed as an argument?

No and yes.

If you refer to traditional arrays, the ones created with:
Dim myarray$(10,10) ;<-- note the () brackets

then the answer is: Nope. Basically you don't need to pass such an array as argument, because arrays are global, that means, the scope of arrays is the whole source code.

If you refer instead to a monodimensional array created inside a type, like:
type Test
field b_arr$[5] ;<-- note the [] brackets 
end type
Global t.test


Then this has sense:
t.test = new test
add_surname(t,"Flinstone") ;call your function

function add_surname(temp.test,name$)
temp\b_arr[0] = temp\b_arr[0] + name
end function


Hope this has sense for you,
Sergio.


soja(Posted 2003) [#3]
So in other words, in order to pass a "BlitzArray" (Array[]) into a function, it must be enclosed within a Type. Correct?


Koriolis(Posted 2003) [#4]
No, in fact you can directly pass such arrays:
Local a_outside[10]
a_outside[2] = 7
a_outside[5] = -3

Function f(a_inside[10])
	For i = 0 To 10
		Print a_inside[i]
	Next
End Function

f(a_outside)
BTW, I just found a nice bug:
Local a[10]
Print Str(a)
->memory access violation.


NewtSoup(Posted 2003) [#5]
Aha, thanks semar and koriolis, what I wanted to do was pass the whole array, I mentioned blitz array to try and make the distinction from a normal array, given that said array can be created on the fly and its size can be dependant on another variable I needed to pass the whole array and not wrap it in a type, if I had to wrap it up then I would have just created a linked list and passed the head into the function but koriolis' code has shown me the light, or at least the syntax to pull it off.
But wait, in koriolis' code the function specifies the size of the array to be passed.. oh no....

a quick test:
test(5)

Function test(i)
	Local arr[i]
End Function 


results in the error message blitz array sizes must be constant.. gahhh so I cant do waht I was plannign anyway...
create an array sized on amount of data
pass array to a fucntion for processing
where the array size and amount of data can only be ascertained at runtime.. oh well back to a linked list of types..
type link
field value
field child.link
field parent.link
end type

oh stupid me, I'll use a bank and pass its handle..


Thanks anyway people much appreciated, at least I know I can pass an array to a function provided I specify its size at compile time.

- Luke - beats himself severly around the head with the blitz 3d manual..