JaxMusic wrote:...how you can reference functions in other code (.pb ) files. For example, I would like to write a series of related functions and keep them in a code file separate from the main code file. How are the references to those functions in the other file declared?
Quite simple, actually. Consider this example:
example 1:Code: Select all
Procedure add(a, b)
ProcedureReturn a + b
EndProcedure
Procedure subtract(a, b)
ProcedureReturn a - b
EndProcedure
Debug add(1, 2)
Debug subtract(6, 3)
To save the two procedures
(add & subtract) separately, simply paste them into another code file, save them as
myFunctions.pb, for example, and then call them from any other code file.
myFunctions.pb:Code: Select all
Procedure add(a, b)
ProcedureReturn a + b
EndProcedure
Procedure subtract(a, b)
ProcedureReturn a - b
EndProcedure
Now, to use these functions, simply include them into any code file, and call them as usual, like so:
myCode.pb:Code: Select all
IncludeFile "myFunctions.pb"
Debug add(1, 2)
Debug subtract(6, 3)
Multiple files can be included in this manner. Let's add another:
myFunctions2.pb:Code: Select all
Procedure multiply(a, b)
ProcedureReturn a * b
EndProcedure
Procedure divide(a, b)
ProcedureReturn a / b
EndProcedure
Then simply add it to the code file to use its functions:
Code: Select all
IncludeFile "myFunctions.pb"
IncludeFile "myFunctions2.pb"
Debug add(1, 2)
Debug subtract(6, 3)
Debug multiply(2, 3)
Debug divide(12, 2)
The
IncludeFile function simply
inserts the referenced code file directly where it is called. Technically, when expanded by the compiler, the code in
myCode.pb would look exactly as the code in
example 1.
It should be noted, however, that if a file is included further down the code list, any preceding calls to any functions within that file will not work. This is because all definitions for arrays, variables, structures, procedures, etc., must precede their reference
(see Question 2 in this post for a more detailed explanation).
This sequence will fail with exception:
Code: Select all
; the compiler will throw an error > add() is not a function...
Debug add(1, 2)
Debug subtract(6, 3)
IncludeFile "myFunctions.pb"
This can be remedied by simply declaring the required functions prior to their calls, though it may not be practical for lengthy includes.
Code: Select all
Declare add(a, b)
Declare subtract(a, b)
Debug add(1, 2)
Debug subtract(6, 3)
IncludeFile "myFunctions.pb"