Take a look at the most recent TGC newsletter. There is a great article in there about functions.
A lot of code use in DB are functions, they are just hardcoded into the programming language. Some examples are:
Object Position X()
MouseX()
RGB()
Basically, anything with parenthesies "()" after is a function. User Defined Functions are just as useful. You can use them to plug in a couple of values and return a single result. For example:
b#=4
a# = half(b#)
Print a#
Wait key
End
Function half(n#)
v#=n#/2.0
Endfunction v#
This code returns the number in the brackets "()" divided by 2.
This is a very basic function, so it doesn't seem to useful. But, if it were more complex, calculating angles, trig, If/Then, whatever. Functions can even call other functions. Example:
Function Even_Odd(n#)
if half(n#) = int(half(n#) Then e$="Even" else e$="Odd"
EndFunction e$
This code, using the last code's "half()" function and returns whether the number is even or odd.
Why use a function over gosub/return? There are arguments that go back and forth about which is better, but I like functions best because they are neat and easy to debug. The compiler will tell you right away if you you forgot to use EndFunction or to return a value. Gosub/Return aren't tracked nearly as neatly by the compiler. If you forget to plug in a return, your code might actually compile and work for a while, but not the way you'd like it too. You could also end up using up all your system's memory.
"Droids don't rip your arms off when they lose." -H. Solo
REALITY II