Good evening. As I explore Pure Basic I am trying to get a handle on code organization. I see we can have multiple code files in a program and can access them in the IDE, or even organize them in a project. What I don’t see is 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? Is there something equivalent to #include x.pb to use in your main flow?
Thanks
Code Organization
Re: Code Organization
Sure is:JaxMusic wrote: there something equivalent to #include x.pb to use in your main flow?
https://www.purebasic.com/documentation ... ludes.html
So you can create all the common reusable code in such a PB file, and then include them in any source at the point of the IncludeFile() command.
Re: Code Organization
2 schools.
Use IncludeFiles of your xxx_something.pbi code or use Modules.
I prefer the former method.
This does demand a prefixed naming convention for large projects, but there is no additional syntax burdens.
You can also use both methods when the community provides libraries in module or includefile format.
Use IncludeFiles of your xxx_something.pbi code or use Modules.
I prefer the former method.
This does demand a prefixed naming convention for large projects, but there is no additional syntax burdens.
You can also use both methods when the community provides libraries in module or includefile format.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Re: Code Organization
Quite simple, actually. Consider this example: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?
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)
myFunctions.pb:
Code: Select all
Procedure add(a, b)
ProcedureReturn a + b
EndProcedure
Procedure subtract(a, b)
ProcedureReturn a - b
EndProcedure
myCode.pb:
Code: Select all
IncludeFile "myFunctions.pb"
Debug add(1, 2)
Debug subtract(6, 3)
myFunctions2.pb:
Code: Select all
Procedure multiply(a, b)
ProcedureReturn a * b
EndProcedure
Procedure divide(a, b)
ProcedureReturn a / b
EndProcedure
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)
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"
Code: Select all
Declare add(a, b)
Declare subtract(a, b)
Debug add(1, 2)
Debug subtract(6, 3)
IncludeFile "myFunctions.pb"
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel 

Re: Code Organization
I prefer XIncludeFile, because of you have a large project with multiple shared files, you don't accidentally send up with duplicates.
For instance, if both library A and library B require library C to work, in a project that requires both A&B, using normal IncludeFile in each could cause errors due to the duplicate includes. But XIncludeFile avoid/prevents that issue.
For instance, if both library A and library B require library C to work, in a project that requires both A&B, using normal IncludeFile in each could cause errors due to the duplicate includes. But XIncludeFile avoid/prevents that issue.
-
- User
- Posts: 20
- Joined: Sun Feb 14, 2021 2:55 am
- Location: Max Meadows, Virginia, USA
- Contact:
Re: Code Organization
[quote="TI-994A"][/quote] thanks for the excellent example TI-994A
Re: Code Organization
Include / XInclude
If you want or need to make your eventual executable small, keep your individual Included files small - any code in them which is not actually used in the current context is still compiled and included in the executable.
Naming convention
Think about this early on. I name anything in a file-to-be-included which has external exposure (procedures, constants, variables) using a prefix unique to that file which is related to the file name (I use a trigraph of capital letters). If it is within the scope of the IDE editor, auto-complete then prompts with a list of possible completions, and the IDE knows procedure parameters just as with PB-native names.
Trying out snippets
Do you want to try out a self-contained bit of code, a demo of something you may want to use, before incorporating it into your project? Perhaps there is more than one possibility and you don't want to touch your already working good stuff before deciding between them. Click on File > New in the IDE and paste in what you want to try out. As long as it is truly self-contained - meaning it has a main loop, as most example code does - you can run it without affecting the main project you are coding so you can see what it does. Very useful when venturing into unknown territory.
If you want or need to make your eventual executable small, keep your individual Included files small - any code in them which is not actually used in the current context is still compiled and included in the executable.
Naming convention
Think about this early on. I name anything in a file-to-be-included which has external exposure (procedures, constants, variables) using a prefix unique to that file which is related to the file name (I use a trigraph of capital letters). If it is within the scope of the IDE editor, auto-complete then prompts with a list of possible completions, and the IDE knows procedure parameters just as with PB-native names.
Trying out snippets
Do you want to try out a self-contained bit of code, a demo of something you may want to use, before incorporating it into your project? Perhaps there is more than one possibility and you don't want to touch your already working good stuff before deciding between them. Click on File > New in the IDE and paste in what you want to try out. As long as it is truly self-contained - meaning it has a main loop, as most example code does - you can run it without affecting the main project you are coding so you can see what it does. Very useful when venturing into unknown territory.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)