Introduction to FreeBasic

As outlined in the introduction, the goal of this section is simply to outline the use of some of the  programming languages utilised within this website, i.e. JavaScript and now FreeBasic. Initially, a Freebasic program was seen as a way of producing animations without the time consuming overhead of producing large (.gif) or (.avi) files. However, to embed an animation into a web page that requires the web user to run an executable (.exe) file may, yet again, raise understandable security concerns. Therefore, this discussion starts by providing another ‘quick-start’ user guide to installing the FreeBasic  Integrated Development Environments (IDE) and compiler into a system, after which the user can copy, check, edit and compile any FreeBasic source code (.bas) files on offer.

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.

  1. Screen Displays
  2. Program Structure
  3. Keyboard Input
  4. Program Control
  5. File Display
  6. File I/O
  7. Basic Graphics
  8. More Graphics
  9. Animations  
  10. 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.

Variable Scope

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.

Data Types

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:

Data Type Size Limits
Byte 8-bit.
 signed,
1 byte
-128
to
+127
Double 64-bit,
floating point,
8 bytes
-2.2E-308
to
+1.7E+308
Integer 32-bit,
signed,
4 bytes
-2,147,483,648
to
+2,147,483,647
Short 16-bit,
signed,
2 bytes
-32,768
to
+32,767
Single 32-bit,
floating point,
4 bytes
1.1 E-38
to
3.43 E+38
Pointer 32-bit,
memory address,
4 bytes
Must be
initialized
at runtime

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.

Variable-length
DIM vs AS STRING
vs = "Hello
"

alternative declaration
DIM vs$
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.