Passing types to functions

Types are just like any other variables in that they can be passed into functions as parameters. Here's a small example program that shows how it is done.

type point
	x as integer
	y as integer
endtype

p as point
UpdatePoint ( p )
do print ( p.x ) print ( p.y )
sync ( ) loop
function UpdatePoint ( a as point ) a.x = 5 a.y = 10 endfunction

Here's how it works.

If you run this program you will see 0 and 0 displayed on screen and not 5 and 10 as you might expect. The reason for this is that by default AppGameKit Studio takes parameters being passed into functions and copies their data, resulting in the parameter being a complete copy of the data passed in. The original data remains untouched by the function.

If you want to modify the data being passed into the function then the parameter needs to be declared slightly differently - use the ref keyword after the name of the variable and before the as keyword. Here's the same program, but this time the type is passed in as reference, resulting in its data being modified directly by the function.

type point
	x as integer
	y as integer
endtype

p as point
UpdatePoint ( p )
do print ( p.x ) print ( p.y )
sync ( ) loop
function UpdatePoint ( a ref as point ) a.x = 5 a.y = 10 endfunction

When you run this program the values 5 and 10 will be displayed on screen because the parameter has been declared using ref, therefore a copy is not made and the data being passed into the function will be directly modified by the function.

To pass in an array of types to a function the parameter must have opening and closing brackets used after it, with no number specified inbetween. Here's a program that shows how it works.

type point
	x as integer
	y as integer
endtype

p as point [ 10 ]
UpdatePoint ( p )
do sync ( ) loop
function UpdatePoint ( a as point [ ] ) for i = 1 to a.length a [ i ].x = 1 a [ i ].y = 2 next i endfunction

Remember that by default the parameter being passed in is a copy of the parameter's data, so modifying it within the function has no affect to the original data. If you want the function to be able to directly modify the data being passed in then the ref keyword must be used, as shown earlier. Here's the same program with some modifications to pass the array into the function using reference, therefore its data will be modified within the function.

type point
	x as integer
	y as integer
endtype

p as point [ 10 ]
UpdatePoint ( p )
do for i = 1 to p.length print ( p [ i ].x ) print ( p [ i ].y ) next i
sync ( ) loop
function UpdatePoint ( a ref as point [ ] ) for i = 1 to a.length a [ i ].x = 1 a [ i ].y = 2 next i endfunction

When you run this program it will simply print out a bunch of 1s and 2s.