Introduction to FreeBasic
However, it is suggested that only after having gained some confidence of the integrity of the FreeBasic source on offer, should the user then proceed to run the FreeBasic programs. So first some general information about Freebasic and where to get it on the Internet. Following the FreeBasic Official Website link will allow you to read up on the details of the various compilers and IDE options on offer for all the different operating systems - see insets right for more details. Essentially, FreeBASIC is a free open source BASIC compiler for Microsoft Windows, DOS and Linux. However, in order to develop and compile basic source (.bas) files, you will probably want to install the Integrated Development Environments (IDE), which then allows you to edit the (.bas) source file and invoke the compiler through a more convenient user interface.
Note: you will also need to install the FreeBasic compiler for your operating system before you can compile and run any programmes, i.e. .exe file. Again, further details can be found on the FreeBasic website linked above and right.
Of course, even assuming that you complete this stage, the next problem is getting started with the process of learning how to code in FreeBasic. Based on my own experience, learning to use any programming language generally follows the 80/20 rule, i.e. you can learn 80% of what you need to know in about 20% of the time. The remaining 20% that you do not need straight away will take you the other 80% time-wise, even if attempted. Therefore, what most people need is a ‘quick-start’ introduction of the most salient issues, which then allows them to start writing a few programs. After this you can simply add to your knowledge on a ‘need-to-know’ basis. In this context, the following list represents a 10 step guide in the form of simple programs that cover some of the essentials of FreeBasic.
- Screen Displays
- Program Structure
- Keyboard Input
- Program Control
- File Display
- File I/O
- Basic Graphics
- More Graphics
- Wave Simulator
The links above point to a brief outline of each program that in-turn provide links to the source code and executable programs. However, as stated, while I know the FreeBasic .exe files on this website are not intended to be malicious to your system, you should always be cautious of loading such files from any website that you do not know or trust. However, before jumping into to these example, it may be worth just introducing some basic data types.
The scope of a variable refers to its visibility or accessibility within a program. A ‘local variable’ is defined at the module level and is visible within the module level code, but not to any subroutine or function within the module. By the same token, variables defined within a subroutine or function are local to the subroutine or function and are not visible to module level code or any other subroutine or function. However, a variable may be defined as ‘shared’ provided its name is unique within the global scope of all modules, subroutines and functions. There are many aspects to the declaration of various types of variable, but the basic idea of local and shared is enough to get started.
The subject of data types within a programming language is another topic that can quickly expand such that the detail can overwhelm a person simply wanting to get started. As such, the following table will restrict its outline to the basic numeric and string data types and therefore while far from a complete definition of even the numeric data types, it is probably enough for most people to get started:
The following examples are essentially generic to all numeric data types and use the INTEGER type for comparison.
Dim myVar As Integer
This will create a single integer-type variable.
Dim As Integer myVar, myVar2
This will create two integer-type variables. While defining multiple variables on the same line is OK, remember about comments so that other people, and yourself, can understand the purpose of the variable.
Dim myVar as Integer = 5
This will create an integer-type variable and set the value of the variable to 5. Some compilers do not like the declaration and initial value assignment to be linked. From a structural viewpoint, it can be argued that declaration of all variables might be done in separate include file, while the initialisation of the variable is carried out at some appropriate within the source code.
The string data type is also essential to even the most simplest of programs, such as the “Hello, world” example in screenDisplay, because text strings are invariably required to communicate any sort of result to the program user. However, computers really only understand numbers and therefore text data is always mapped to some sort of numeric equivalence code, although we do not need to understand such details at this stage. Therefore, with the ‘get started’ goal in mind, we shall only outline 2 basic ideas linked with dynamic and fixed strings. Dynamic strings are variable-length strings that the compiler automatically allocates and resizes as needed, e.g.
DIM vs AS STRING
vs = "Hello"
vs$ = "World"
This alternative declaration is shown simply because some ‘dialects’ of the BASIC language require the [$] identifier
Fixed-length 32 byte string
DIM fs AS STRING * 32
fs = "Hello World"
As a general statement, the quick-start examples will essentially focus on the use of variable length strings, the manipulation of text strings can also raise the topic of buffer array and string pointers. However, introducing this level of detail upfront can be counter-productive as they are topics best addressed after you are up and running. So now might be a good time to take a look at some examples.