Hello,
If you want to use a variable that can be used inside and outside of functions, you can use an array. When an array is defined, it is actually reserving a little chunk of memory for it to use that doesn't get erased until it is undimensioned, so it sticks around even when used by functions. You can also get fancy and use memblocks which are really just user defined arrays or structures.
Anyway, here's an example of using an array as a global variable and then changing it inside a function. Note that I don't even pass it as a value to the function, but I operate on it inside of the function and therefore it changes. Also note that the function doesn't pass it back out - but yet a(0)s value is still changed.
dim a(0)
a(0)=65467
test()
print a(0)
end
function test()
a(0)=123
endfunction
Here's the same code but without using an array:
a=65467
test()
print a
end
function test()
a=123
endfunction
Note that the global value of a is not affected by the function.
If we aren't using an array, then to change the value of a, the function has to output a value, and we have to capture that value:
a=65467
a=test()
print a
end
function test()
a=123
endfunction a
Testing for odd or even is easy! Just divide by two and check for a fractional leftover.
This function will test an integer for being even or odd. a value of 0 returned means even. A value of 1 returned means odd.
do
cls
input "Enter a value to test: ";number#
if even_or_odd(number#) = 0
center text 100,100,"User entered "+str$(number#)+" is even."
else
center text 100,100,"User entered "+str$(number#)+" is odd."
endif
center text 100,140,"Press Enter..."
suspend for key
loop
end
function even_or_odd(num#)
result=0
test=int(num#/2)
if abs(test) < abs(num#/2) then result=1
endfunction result
Enjoy your day.