Fundamentals of the PureBasic Language
Before we start writing this first program in PureBasic, there are a few fundamentals that you
should know about PureBasic and always keep in your mind. These are used everywhere in PureBasic
and will save you a lot of trouble if you remember them. The PureBasic source code is a language
which has rules that must be followed if you want it to be correct, just as with any other
language (for computers or humans).
General layout of a PureBasic program
A program written in PureBasic consists of one or more files containing the source code
(the instructions that the computer carries out) for your program. The compiler
reads these files and produces an executable which you can run like any other piece
of software on your computer. If there are any errors in your source codes you will be
told about them and an executable will not be created.
Usually you will write one instruction on each line, but you can use the colon character
to separate instructions to put them on the same line. For example, the two sets of
instructions below are equivalent (do not worry about what the instructions do at this point):
OpenConsole()
For i=0 To 10
a = a + 2
PrintN("Hi there "+Str(a))
Next
OpenConsole() : For i=0 To 10 : a = a + 2 : PrintN("Hi there "+Str(a)) : Next
Although you can put many instructions on one line like this, you cannot split an instruction
over a number of lines.
Types of instructions
As mentioned above, PureBasic source code is made up from sequences of instructions
for the computer to execute. These instructions can be:
- simple manipulations of variables - variables are a place to store and manipulate data
- more complex PureBasic keywords - keywords are instructions which are built into
the language. Keywords will be highlighted in bold text in the PureBasic editor (as a
handy aid to help you identify them).
- or commands - commands are sequences of instructions which give your programs their most useful
abilities and are all contained in the "PureLibraries" files.
Keywords
The general format which keywords follow is:
<Keyword
name> [Keyword data]
which is the keyword name followed by
whatever the keyword is working on. Of course, the exact format of the instruction
depends on the keyword. These will be explained as they are reached in this
User Guide.
You should notice that keywords do not have parenthesis (brackets) after them.
An example of a keyword is the For keyword in the above example.
Commands
Commands always follow the format shown below:[return value] <Command name>([parameter 1 [, parameter 2 [, parameter 3 [, ... [, parameter n]]]]])
Command names are always followed by an opening parenthesis (bracket), which must have
a matching closing parenthesis somewhere later. Commands are said to take
parameters and return a result, both of which are simply a method
of passing information from your source to the instructions, which make up
the command, and back again.
Parameters are written inside the parentheses and
each parameter is separated by a comma. If there are no parameters then you do
not put anything between the parentheses. The meaning of each parameter is
completely dependent on the command.
The return value is a value which the command returns to your source code and
can be used in calculations and manipulated just like any other kind of value in
PureBasic. The return value is optional, so you may not need to use it; in this
case you simply start the instruction with the command name. What the return value
means is completely dependent on the command.
An example of a command is the PrintN command in the above example.
Comments
The semi-colon character is used to indicate a comment or remark.
Any text from the semi-colon to the end of the line is ignored by the PureBasic compiler,
allowing you to add comments to your programs so that you can remember why you done
something, or to explain an amazing piece of code that you wrote.
Strings and numbers
Strings (sequences of characters) which do not change during the execution of
your program are written by enclosing the string in inverted commas, the
" character. An example of a string is "Hi. I am a string".
Numbers in your program which do not change can be written simply as the
number, for example, 34. If you need to have a number with a fractional part
you can simply write, for example, 123.76.
Execution of source code
Your program starts executing from the first instruction in your source code and moves
step by step through it until the end of the program. Of course, you can also
change the flow of your program by making decisions, repeating instructions
and specifically executing other parts.