Begins a standalone snippet of code that is callable from your program. |
name = any valid name text that is not a keyword of Blitz. |
Command Description:
A function is a routine of commands you may choose to call frequently within your program. Functions are considered 'proper' practice in this situation, instead of using GOSUB commands. Functions are independant of your 'main code' and will only execute if they are called. They have their own namespace, and variables created outside a function are NOT available within the function (this goes for TYPE structures as well) unless you declare the variable or Type structure as GLOBAL. You can pass variables into functions, as well as RETURN boolean values (True/False) back to the calling code. The practical use of functions is to help seperate your code into managable chunks for subsequent perfection of a routine. You may put all your screen updates into a single function, for example. Or perhaps your scoring system where you pass it what alien has been destroyed and it updates the player's score. Once the function reaches the END FUNCTION command, it returns program execution to the next line following the function call. Functions can be a bit daunting until you realize they really are their own little programs within your program. See the example for more. |
Example:
; Function Example ; Get the user's name name$=Input$("Enter Your Name:") ; Call a function to print how many letters the name has numletters(name$); ; Let's get something BACK from the function thefirst$=firstletter(name$) ; Now print results Print "Was the first letter an 'S'? (1=True/0=False)" + thefirst$ ;The program basically ends here, because functions don't run unless called. ; The actual function Function numletters(passedname$) Print "Your name has " + Len(passedname$) + " letters in it." End Function ; Function to see if the first letter is S Function firstletter(passedname$) ; If the first letter is an 'S' then return from the function a true value If Left$(passedname$,1) = "S" Then Return True ; Otherwise, return false Else Return False End If End Function |