Variables used within a function are unique to that function.
For example;
function func_myfunction( )
var_myvariable = 10
print ( "MyVariable: " str(var_myvariable) )
endfunction
This variable will only be usable within that function. To make a variable usable within and utilize the same variable and its value outside of a function you need to declare it a global variable like following example.
global var_myvariable as integer
var_myvariable = 10
function func_myfunction( )
print ( "MyVariable: " str(var_myvariable) )
endfunction
do
func_myfunction( )
sync( )
loop
Now you can use it where ever you want without fear of losing its value that includes gosubs, functions, loops etc.
Here's what the AppGameKit documentation say's on functions and variables within them.
Functions are blocks of commands that usually perform a recursive or isolated task that is frequently
used by your program. Variables used within the function are isolated from the rest of the program.
If you use a variable name of FRED in your function, it will not affect another variable called FRED in
your main program, nor any other function that happens to use a similar variable name. This may seem to
be a restriction, but forces you to think about cutting up your program into exclusive tasks which is a
very important lesson.
More information on Functions at the AppGameKit Documentation
page.