; BlitzBasic(tm) Full Foundation Tutorial ; Language Summary / Reference Guide ;------------------------------------------------------------------------------- ; Guidelines: ; ; * Think logically. ; * Go one step at a time. ; ;== Introduction To Variables ================================================== ; ; Stick something in a variable: Zero = 0 ; Copy the contents of a variable: Nothing = Zero ; Adding the contents of two variables: Dangerous = MadMan + Gun ; Increasing a variable's value: Value = Value + 1 ; Divide a variable's value by 2: Value = Value / 2 ; Stacking maths: Fruit = Apple + Banana + Orange + Pear Complicated = Angle * 180 / Pi - Pi * Radius ^ 2 ; Swapping the contents of two variables: ; One --------------> Two SwapSpace = Two ; /|\ 2 | Two = One ; | | One = SwapSpace ; +---- SwapSpace <---+ ; 3 1 ;== Single Basic Variables ===================================================== ; ; Variables are created automatically when they are used for the first time. ; ; All variables are by default Integer. ; But you can specify of which type they have to be: ; ; [ Name ][ Type ] ; ; Available types: ; ; % Integer variable ; # Floating point variable ; $ String variable ; .[Type name] Pointer variable ; ; Examples: Circle# = 3.14159 ; Float Loaded% = 1 ; Integer Name$ = "TC" ; String (not thong) ; A variable its type cannot be changed after it is first called upon. ; This is also why you do not have to include the variable type identifier ; every time. ; ; You can declare a variable for the first time without assigning a value to it. Local Empty_Variable% ; By default all newly created variables will be set to 0 (or empty). ; ; Each variable has a certain range in a program. ; The Local range means that the variable can be accessed ; only in the current routine. ; ; When an undeclared variable is used for the first time ; it is always declared as a Local variable. ; ; Global variables can be accessed from anywhere. ; ; You can also combine a declaration with an assignment. Global PhoneCalls_SinceYesterday# = 11.5 ; Once you've declared a variable, you cannot change ; its type (integer, float, etc) nor its range (local or global). ; ; There are also so called Constants. ; You put something in them, but you cannot change it afterwards. ; So not exactly variables, but you can use them as such. ; ; In fact, they behave exactly like Global variables. ; So you can use them anywhere in your program. Const Square# = 1.41421 ; Float Const Empty% = -1 ; Integer Const Nick$ = "Ty" ; String ; You can even put maths inside a constant, ; so long as the values are constant as well. Const Zero = 0 Const One = Zero + 1 ; Integer variables can hold numbers ; in the range of -2,147,483,648 and 2,147,483,647. ; Using values beyond this range will cause the value to flip to the other side. ; ; Floating point variables can hold pretty much anything, ; but the more numbers you want to store, the less accurate it will get. ; ; String variables are completely dynamic so they can be virtually any size. ; ; Simply use some glue to add something. Name$ = "Dr. " + "Strangelove" ;== Conditions ================================================================= ; ; True is equal to 1. Condition = True ; False equals 0. Condition = False ; The Not keyword will turn any value not equal to zero into False (zero). Condition = Not Condition ; You can put a comparison in a variable. ; If the comparison is true, the resulting value will be 1 (True). ; And if it is false, the resulting value will be 0 (False). Result = Value > Dinges ; Any result of comparison or value can be used as a condition. Condition = Value ; An Action will be executed when the resulting Condition is non-zero. ; That means anything not equal to False (zero). If Condition Then Action ; Comparisons generally use one of these: =, <, >, >=, <= or <>. ; ; You can even combine maths with comparisons, ; and there are a lot of various other keywords (Not, And, Or, etc) ; and operators (+, -, /, *, etc) you can use. ; ; You should find a list of all these with a lot of more technical details ; in the language reference which can be accessed from the first screen you see ; when you start Blitz. ; ; There's a way you can execute multiple actions when a condition is met. If Condition Then Action Action End If ; 'Then' is completely optional. ; ; You can also specify actions when the condition was not met. If Condition Then Action1 Else Action2 If (Condition) Action1 Else Action2 End If ; Brackets () are optional and can improve readability. ; You might also need them to force the order of evaluation. ; ; Comparisons, maths, etc are handled, calculated, evaluated in specific order. ; For example, in maths, multiplication has priority over addition. Result = ( 10 + 10 ) * 10 ; You can stack IFs: If Condition1 Then Action Action Else If Condition2 Then Action Action Else If Condition3 Then Action Action Else Action Action End If ; Some examples of range checking: If Value >= Minimum If Value <= Maximum Print "In range!" End If End If If ( Value >= Minimum ) And ( Value <= Maximum ) Print "In range!" End If ; Comparing a variable's value with a number of values in a list: Select Value Case 1 : Print "One" Case 2 : Print "Two" Case 3 : Print "Three" Default : Print "Something else" End Select ;== Loops ====================================================================== ; ; Endless loop: Repeat Action Forever ; The Exit command will escape the currently executing loop. ; So the 'program cursor' will skip to the first (action) ; under the Forever statement. Repeat Exit;------+ Forever ;| ; / | ;<-------------+ ; \ ; Count from one to ten using a While..Wend loop. ; When the Condition is equal to False, the loop is aborted. ; So when the Condition is True, the loop continues. Value = 0 While Value < 10 Value = Value + 1 Print Value Wend ; Using a Repeat..Until loop. ; When the Condition is False, the loop repeats. ; So when the Condition is equal to True, the loop will end. Value = 0 Repeat Value = Value + 1 Print Value Until Value = 10 ; Using a For..Next loop. For Value = 1 To 10 Print Value Next ; You can even specify a stepping value, although it has to be constant ; so can't use a variable for a stepping value. For Floating# = 0 To 1 Step 0.1 Print Value Next ; Using a stepping value in a For..Next you can also count backwards. For Value = 10 To 1 Step -1 Print Value Next ; If you try to count backwards without a stepping value, ; the actions in the loop simply won't be executed. ; ; You can use Goto to jump to a specific point in your program. ; A point, marked by a Label (note the dot). ; Note that it can turn your code easily into spaghetti. .Label Goto Label ; You can use colons (:) to glue code. ; The code glue doesn't always work the way you want it to though. ; And it can make reading code really hard. ; You can even glue code without glue! ; ; Types of glue have a certain order in which they work. ; Example: Value1 = 10 Value2 = 10 Print "|" + Value1 + Value2 + "|" ; Text glue Print "|" + ( Value1 + Value2 ) + "|" ; Number glue ; This is where you can find a list of these rules Blitz uses ; from the first page you see when you open Blitz: ; ; Language Reference -> Expressions ; ; You can also combine as many loops ; of the same (or different) kind ; as you want. For Outer = 1 To 2 For Inner = 1 To 2 ; <--- Next Next ; For example to create a grid: Const GridSize = 5 For Y = 1 To GridSize Print String ( "+-" , GridSize ) For X = 1 To GridSize Write "| " Next Print "|" Next Print String ( "+-" , GridSize ) ; Or a checkerboard: Even = False Print "+----------+" For Y = 1 To 10 Write "|" For X = 1 To 10 Even = Not Even If Even Then Write "O" Else Write "·" Next Even = Not Even Print "|" Next Print "+----------+" ;== Variable Collections ======================================================= ; ; Declaring an array: Dim Array( 4 ) ; Assigning values to the elements. Array( 1 ) = 50 Array( 2 ) = 0 Array( 3 ) = 0 Array( 4 ) = 5 ; Walking through each element of the array using a For..Next loop: For Element = 1 To 4 Print Array( Element ) Next ; Using a Repeat..Until loop. Element = 0 Repeat Element = Element + 1 Print Array( Element ) Until Element = 4 ; Using a While..Wend loop. Element = 0 While Element < 4 Element = Element + 1 Print Array( Element ) Wend ; By default, all variables will be set to 0 when created. ; This also applies to arrays. ; By default, all variables will be of type integer. ; This also applies to arrays. ; ; When you redefine an array everything inside the array will be set to 0 again. ; You can even change its size: Dim Array( 2 ) ; When an array is created it doesn't start counting from 1. Array( 0 ) = 50 Array( 1 ) = 5 ; There are actually 3 elements in it, when you define it with ( 2 ). ; So there's an extra element ( 0 ). ; This is the difference between 0-based and 1-based orientation. ; E.g. start counting from 0 instead of 1. ; ; You can see the array as: ; ; | 0 | 1 | 2 | - Element (array) ; +-------+-------+-------+ ; | | | | ; | 50 | 5 | 0 | - Contents (value) ; | | | | ; +-------+-------+-------+ ; | Array | ; +-----------------------+ ; ; Arrays are used like this: ; ; [ Name ][ Type ] ( [ Elements ] ) ; ; Unlike normal variables, normal arrays - declared using the Dim keyword - ; are always Global. So you can use them anywhere in your program. ; ; You can also create multi-dimensional arrays. Dim Grid( 6 , 4 ) ; | 0 | 1 | 2 | 3 | 4 | 5 | 6 | ; ---+-------+-------+-------+-------+-------+-------+-------+ ; | | | | | | | | ; 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ; | | | | | | | | ; ---+-------+-------+-------+-------+-------+-------+-------+ ; | | | | | | | | ; 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ; | | | | | | | | ; ---+-------+-------+-------+-------+-------+-------+-------+ ; | | | | | | | | ; 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ; | | | | | | | | ; ---+-------+-------+-------+-------+-------+-------+-------+ ; | | | | | | | | ; 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ; | | | | | | | | ; ---+-------+-------+-------+-------+-------+-------+-------+ ; | | | | | | | | ; 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ; | | | | | | | | ; +--+-------+-------+-------+-------+-------+-------+-------+ ; | Grid | ; +----------------------------------------------------------+ ; ; Placing stuff on the grid. Const Free = 0 Const Wall = 1 For Y = 0 To 4 Grid( 0 , Y ) = Wall Grid( 6 , Y ) = Wall Next For X = 0 To 6 Grid( X , 0 ) = Wall Grid( X , 4 ) = Wall Next ; | 0 | 1 | 2 | 3 | 4 | 5 | 6 | ; ---+-------+-------+-------+-------+-------+-------+-------+ ; | | | | | | | | ; 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | ; | | | | | | | | ; ---+-------+-------+-------+-------+-------+-------+-------+ ; | | | | | | | | ; 1 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | ; | | | | | | | | ; ---+-------+-------+-------+-------+-------+-------+-------+ ; | | | | | | | | ; 2 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | ; | | | | | | | | ; ---+-------+-------+-------+-------+-------+-------+-------+ ; | | | | | | | | ; 3 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | ; | | | | | | | | ; ---+-------+-------+-------+-------+-------+-------+-------+ ; | | | | | | | | ; 4 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | ; | | | | | | | | ; +--+-------+-------+-------+-------+-------+-------+-------+ ; | Array | ; +----------------------------------------------------------+ ; ; Walking through each element of a 2 dimensional array: For Y = 0 To 4 For X = 0 To 6 Write Grid( X , Y ) ; Same line. Next Print ; Next line. Next WaitKey ; An example of a three dimensional array of strings: Dim Grid3D$( 10 , 10 , 1 ) ; That would be.. 11 * 11 * 2 = 242 elements. ; ; ---+---+---+---+---+---+---+---+---+---+---+---+ ; 1 / / / / / / / / / / / /| ; ---+---+---+---+---+---+---+---+---+---+---+---+ + ; 0 / / / / / / / / / / / /|/| ; ---+---+---+---+---+---+---+---+---+---+---+---+ + + ; 0 | | | | | | | | | | | |/|/| ; ---+---+---+---+---+---+---+---+---+---+---+---+ + + ; 1 | | | | | | | | | | | |/|/| ; ---+---+---+---+---+---+---+---+---+---+---+---+ + + ; 2 | | | | | | | | | | | |/|/| ; ---+---+---+---+---+---+---+---+---+---+---+---+ + + ; 3 | | | | | | | | | | | |/|/| ; ---+---+---+---+---+---+---+---+---+---+---+---+ + + ; 4 | | | | | | | | | | | |/|/| ; ---+---+---+---+---+---+---+---+---+---+---+---+ + + ; 5 | | | | | | | | | | | |/|/| ; ---+---+---+---+---+---+---+---+---+---+---+---+ + + ; 6 | | | | | | | | | | | |/|/| ; ---+---+---+---+---+---+---+---+---+---+---+---+ + + ; 7 | | | | | | | | | | | |/|/| ; ---+---+---+---+---+---+---+---+---+---+---+---+ + + ; 8 | | | | | | | | | | | |/|/| ; ---+---+---+---+---+---+---+---+---+---+---+---+ + + ; 9 | | | | | | | | | | | |/|/ \ ; ---+---+---+---+---+---+---+---+---+---+---+---+ + 1 ; 10 | | | | | | | | | | | |/ \ ; ---+---+---+---+---+---+---+---+---+---+---+---+ 0 ; | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 \ ; ; Or like this: ; ; | 0 | 1 | 2 | 3 | 4 | 5 ; ---+---+---+---+---+---+---+---+---+---+---+-- - - ; | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | ; 0 |---+---|---+---|---+---|---+---|---+---|-- - - ; | : | : | : | : | : | ; ---+-------+-------+-------+-------+-------+-- - - ; | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | ; 1 |---+---|---+---|---+---|---+---|---+---|-- - - ; | : | : | : | : | : | ; ---+-------+-------+-------+-------+-------+-- - - ; | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | ; 2 |---+---|---+---|---+---|---+---|---+---|-- - - ; | : | : | : | : | : | ; ---+-------+-------+-------+-------+-------+-- - - ; | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | ; 3 |---+---|---+---|---+---|---+---|---+---|-- - - ; | : | : | : | : | : | ; ---+-------+-------+-------+-------+-------+-- - - ; | | | | | | ; 4 : : : : : : etc ; ; So if you would say Grid3D( 0 , 0 , 1 ) = 6 ; here's what would happen: ; ; | 0 | 1 ; ---+---+---+-- - - ; | 0 | 1 | ; 0 |---+---|-- - - ; | : 6 | ; ---+-------+-- - - ; | | ; 1 : : etc ; ; By default, the grid is empty, because it's a grid of strings. ; ; You can easily store numbers in text elements or variables. ; But if you try storing text in an integer variable, ; Blitz will attempt to convert it to a number. ; And if everything fails, it will be converted to 0. (eek) ; For example "123ABC" will be converted to 123. ; ; You can force a conversion using the available conversion tools: ; ; Integer Int () ; String Str () ; Float Float () ; ; The brackets are optional. ; ; At the end of a For..Next loop, the iterator variable will equal ; the maximum value plus one iteration increment; For Iterator = 1 To 10 Next ;<-- Iterator equals 11 ; You can use Data statements to store data right there in your program. .Start Data 1 , 2 , 3 , 4 , 3 , 2 , 1 ; Blitz will store each data element in sequential order. ; You can use the Restore command to position the data reading cursor ; at the first Data statement under the specified Label. Restore Start ; By default the data reading cursor starts from the top of your program. ; ; Then you can read data elements using the Read command. Read Value ; Reads the next value from data. ; Here's an overview of all variables. ; An integer variable: Integer_Variable% = 1000 ; +---------------+ ; | | ; | 1000 <---- ; | | ; +---------------+ ; | Integer var. | ; +---------------+ ; ; A floating point variable: FloatingPoint_Variable# = 1.000 ; +---------------+ ; | | ; | 1.0 <---- ; | | ; +---------------+ ; | Float var. | ; +---------------+ ; ; A string variable: String_Variable$ = "1.000,00" ; | | | | | | | | ; v v v v v v v v ; | | | | | | | | | ; | 1 | . | 0 | 0 | 0 | , | 0 | 0 | ; +---+---+---+---+---+---+---+---+-- - ; | String variable ; +------------------------------ -- - ; ; An array of integers: Const NumberOfElements% = 3 Dim ArrayOfIntegers%( NumberOfElements ) ; | | | | ; +----- v -----+----- v -----+----- v -----+----- v -----+ ; | | | | | ; | 0 | 0 | 0 | 0 | ; | | | | | ; +---------------+---------------+---------------+---------------+ ; | Integer | Integer | Integer | Integer | ; +---------------+---------------+---------------+---------------+ ; | Array of integer variables | ; +---------------------------------------------------------------+ ; ; An array of floats: Dim ArrayOfFloats#( NumberOfElements ) ; | | | | ; +----- v -----+----- v -----+----- v -----+----- v -----+ ; | | | | | ; | 0.0 | 0.0 | 0.0 | 0.0 | ; | | | | | ; +---------------+---------------+---------------+---------------+ ; | Float | Float | Float | Float | ; +---------------+---------------+---------------+---------------+ ; | Array of floating point variables | ; +---------------------------------------------------------------+ ; ; And finally, an array of strings: Dim ArrayOfStrings$( NumberOfElements ) ; | | | | ; v v v v ; | | | | ; | | | | ; +---+-- - +---+-- - +---+-- - +---+-- - ; | String | String | String | String ; +-----------+-----------+-----------+-----------+ ; | Array of string variables | ; +-----------------------------------------------+ ; ; or ; ; +---------------+ ; | | ; | String 0 <---- ; | | ; +---------------+ ; | | ; | String 1 <---- ; | | ; +---------------+ ; | | ; | String 2 <---- ; | | ; +---------------+ ; | | ; | String 3 <---- ; | | ; +---------------+-----------+ ; | Array of string variables | ; +---------------------------+ ; ; Constants can be used as: ; ; State identifier Const State = .. What_Are_We_Doing = State Select What_Are_We_Doing Case State .. ; Count identifier Const Count = .. NumberOfApples = Count Dim Array( Count ) ; Index identifier Const Index = .. Array( Index ) = Value ; Object collections begin with the Type keyword. ; An object specification for one object: Type Thing Field Contents Field .. End Type ; You can use a pointer (to type) variable to handle objects. Local Pointer.Thing ; When a pointer variable is first created, ; it actually doesn't point to anything, which is called Null. If PointerToPlayer = Null Then Print "Yup, nothing there" ; You can create an existing copy of this Type by using the New command ; followed by sticking it in a variable of the same type. Pointer = New Thing ; (Variable).(Type) = New (Type) ; When the pointer is actually pointing to something, ; you can change the contents of what it's pointing to. Pointer\Contents = 100 ; (Variable).(Type)\(Field) = (Value) ; The backslash (\) allows you to access a field. ; And you can use these fields just like any other variable. ; ; You can delete an object by using the pointer. Delete Pointer ; Delete (Pointer).(Type) ; You can walk through the entire collection of objects ; using a single pointer to Player type variable. ; ; | ; v ; +----- -----+ +---------------+ +---------------+ ; - --| Player object |---| Player object |---| Player object |-- - ; +---------------+ +---------------+ +---------------+ ; ; Like mentioned before, it is actually very similar to ; looping through an array with one integer variable. ; ; | ; v ; +--- ---+ +-----------+ +-----------+ ; - --| Element |---| Element |---| Element |-- - ; +-----------+ +-----------+ +-----------+ ; ; You can use the Each keyword to do this: For Pointer = Each Thing ; For (Pointer).(Type) = Each (Type) Number = Number + 1 Write Number + ": " Print Pointer\Contents Next WaitKey ; You can also do this manually using the First and After keywords ; in for example a Repeat..Until loop: Pointer = First Player ; First (Type) Repeat Pointer = After Pointer ; After (Pointer).(Type) Until Pointer = Null ; Example of walking backwards using the Last and Before keywords ; in a While..Wend loop: Pointer = Last Player ; Last (Type) While Pointer <> Null Pointer = Before Pointer ; Before (Pointer).(Type) Wend ; You can use the Insert keyword to move objects around in the collection. Insert Pointer1 After Pointer2 ; Combining keywords: Insert Last Pointer Before First Pointer ; You don't always have to rely on the keywords First, Last, Before, After etc. ; Pointers will be pointers; you can use them in any way you want. ; ; Linking objects: Type Link Field Connect.Link End Type FirstLink.Link = New Link ; FirstLink ; | ; v ; ,---. ; / \ ; ( ; \ / ; `---' AnotherLink.Link = New Link ; FirstLink ; | AnotherLink ; v | ; ,---. v ; / \ _.---._ ; ( ( ; \ / `^---^^ ; `---' FirstLink\Connect = AnotherLink ; FirstLink ; | AnotherLink ; v | ; ,---. v ; / _>---._ ; ( ( ) ; \ ^>---^^ ; `---' AnotherLink = Null ; FirstLink ; | ; v ; ,---. ; / _>---._ ; ( ( ) ; \ ^>---^^ ; `---' ; ; So long as you keep track of the root or your own first link of the chain, ; you will be able to find any link in the chain without having to resort to ; any of Blitz' native keywords. AnotherLink = FirstLink\Connect ; You can make it really hairy if you wanted to. FirstLink\Connect\Connect\Connect\.. ; Using a While..Wend loop to iterate through all links in the chain: ; This will be our iterator. Local Pointer.Link ; Find our first link. Pointer = FirstLink While Pointer = Null ; Point to next link in chain. Pointer = Pointer\Connect Wend ; There's another type of array called a BlitzArray[tm] ; which can also be used as a Field in an object. Type Inventory Field Slots%[ 10 ] End Type Inv.Inventory = New Inventory Inv\Slots[ 0 ] = 293 ; (Pointer).(Type)\Array[ Element ] = Value ; You cannot redimension it and its size has to be constant. ; ; Also possible is a stand-alone BlitzArray[tm] (outside of an object): Const Amount = 10 Local Weapons[ Amount ] ; Any array can also contain pointers: Type Thing Field Contents End Type Dim PointerArray.Thing( 0 ) ; Dim Array.Type( Elements ) PointerArray( 0 ) = New Thing PointerArray( 0 )\Contents = False ; When we redimension the array, the objects will survive. ; Only the pointers will be erased. ; ; Here's a BlitzArray[tm] of pointers: Global PointerArray.Thing[ 0 ] PointerArray[ 0 ] = Null ; Some examples of text and number glue: Print Value1 + Value2 ; /|\ ; | ; Number glue ; Print Value1 + "" + Value2 ; /|\ /|\ ; | | ; Text glue ; Print Str ( Value1 ) + Str ( Value2 ) ; /|\ ; | ; Text glue ; ; Glueing floats with integers: ; Just integers, not accurate. Value = 10 / 3 * 10 Print Value ; Converted to float, accurate. Value = Float 10 / 3 * 10 Print Value ; Using a float instead of integer, accurate. Value = 10.0 / 3 * 10 Print Value ; Changed priority, accurate. Value = 10 * 10 / 3 Print Value ; Actually, you can do the same with pointers. Type Any Field Thing# End Type pAny.Any = New Any pAny\Thing = -3.14 ; There's the Handle keyword to convert to a number. Value = Handle pAny ; hType = Handle ( Pointer.Type ) Print Value ; Technically what you'll get is a unique memory address for the pointer. ; ; This way you can keep track of pointers by storing their address ; instead of just pointers. ; ; The Object keyword converts the address back to a pointer. pAny = Object.Any Value ; Pointer.Type = Object.Type ( hType ) ; You can also use this approach to test any number against any type. ; ; So you could for example store *different* types of objects ; in a regular array of integers. Write "It's a" If Object.Engine Array( Element ) <> Null Print "n engine." Else If Object.Generator Array( Element ) <> Null Print " generator." Else If Object.Weapon Array( Element ) <> Null Print " weapon." Else If Object.Shield Array( Element ) <> Null Print " shield." End If ; Handle and Object work exactly the same as Int, Str and Float. ; ;== Routines =================================================================== ; ; With functions you can divide your program in constructive blocks. Function Name () Actions End Function ; You can call functions from anywhere. NameTheText ; After everything inside the function is called, Blitz will continue ; with anything under the function call. ; ; | +------------------------+ <---+ ; | | | | ; | | Function | | ; | | Name | | ; | | | | ; | +------------------------+ | ; | | | ; v | | ; | | ; Name ------------------------------+ ; | ; <---------------+ ; ; Local variables inside a function will only be visible (or existent) ; inside that same function. ; ; +-+---------------------------------------+ ; | | | ; | | Main routine | ; | | | ; | | +-+------------------------------+ ; | | | | | ; | | | | Sub routine | ; | | | | | ; | | | | <---- Local Iterator% | ; | | | | | ; | | +-+------------------------------+ ; | | | ; +-+---------------------------------------+ ; ; Global variables can only be declared outside of functions ; and can be accessed anywhere in and out of a function. Global Integer% ; +-+---------------------------------------+ ; | | | ; | | Main routine | <--------+ ; | | | | ; | | Global Integer% ---------------------------+ ; | | | | ; | | +-+------------------------------+ | ; | | | | | | ; | | | | Sub routine | <-----+ ; | | | | | | ; | | +-+------------------------------+ | ; | | | | ; | | | <--------+ ; | | | ; +-+---------------------------------------+ ; ; Blitz prioritizes Local variables over Global variables with the same name. ; ; +-+---------------------------------------+ ; | | | ; | | Main routine | <--------+ ; | | | | ; | | Global Integer% ---------------------------+ ; | | | | ; | | +-+------------------------------+ | ; | | | | | | ; | | | | Sub routine | | ; | | | | | \ / | ; | | | | <---- Local Integer% | X <--+ ; | | | | | / \ | ; | | +-+------------------------------+ | ; | | | | ; | | | <--------+ ; | | | ; +-+---------------------------------------+ ; ; When you define a Local variable in the main code ; it will only be accessible from the main code. ; ; +-+---------------------------------------+ ; | | | ; | | Main routine | ; | | | ; | | <---- Local Value% | ; | | | ; | | +-+------------------------------+ ; | | | | | ; | | | | Sub routine | ; | | | | | ; | | | | <---- Local Value% | ; | | | | | ; | | +-+------------------------------+ ; | | | ; +-+---------------------------------------+ ; ; You can specify parameters and let the function return a value: Function Name ( Parameters ) Local Variables Actions Return Value End Function ; Calling a function with parameters: Name Parameter1 , Parameter2 , .. ; Returning a value from a function call: Value = Name () ; The brackets are not optional when returning a value. ; ; You can also specify parameters. Value = Name ( Parameters ) ; By default a value of 0 (False) is returned when no Return is used. ; With strings, the default return value is "", ; and with pointers it's Null. ; ; Arrays can be redeclared inside a function, if they were declared before ; in the main program. ; ; +-+---------------------------------------+ ; | | | ; | | Main routine | <--------+ ; | | | | ; | | Dim Array( 0 ) ----------------------------+ ; | | | | ; | | +-+------------------------------+ | ; | | | | | | ; | | | | Sub routine | | ; | | | | | | ; | | | | Dim Array( 20 ) | <-----+ ; | | | | | | ; | | +-+------------------------------+ | ; | | | | ; | | | <--------+ ; | | | ; +-+---------------------------------------+ ; ; You can stack evaluations with functions as well: Value = Rand ( x ^ Rand ( x ^ y ) * ToThePowerOf ( x , Rand ( x ^ y ) ) ) ; The parameters you specify actually become local variables ; inside the function. ; ; You can even change the value of parameters (pointers too), ; but they only exist for as long as the function lasts. ; ; A function returning a string type value: Function Name$ () End Function ; Using a pointer as parameter: Function Name ( Pointer.Thing ) Delete Pointer End Function Name New Thing ; Returning a pointer from a function: Function Name.Thing () Return First Thing End Function Delete Name () ; Optional parameters in functions: Function Name ( Value = 38 ) End Function ; You can use a BlitzArray[tm] as a parameter. Const Elements = 1000 Function SetElement ( AnyArray[ Elements ] , Element , Value ) AnyArray[ Element ] = Value End Function ; The specified array when calling the function ; has to be of the size declared for the function. Local Array[ Elements ] SetElement Array , 5 , -1 ; Gosub makes the program cursor jump to a label just like Goto, ; only you can Return to right after the point where you jumped from. Gosub Routine1 Gosub Routine2 End .Routine1 Actions Return .Routine2 Actions Return ;-------------------------------------------------------------------------------