Saturday, March 19, 2011

Variables and Constants, Input and Output, Data Types, Assignment Statements


The understanding and the feelings are moulded by intercourse; the understanding and feelings are corrupted by intercourse. Thus good or bad society improves or corrupts them. It is, then, all-important to know how to choose in order to improve and not to corrupt them; and we cannot make this choice, if they be not already improved and not corrupted. Thus a circle is formed, and those are fortunate who escape it. Blaise Pascal, Pensees, 1669


The reading, writing and modification of variables provides a core function of programming in a manner that is similar to the use variables in algebra. In Pascal a variable is any string of letters and numbers that begins with a letter. It is better programming practise to write a variable that has a meaningful name in context; the variable "InterestRate" makes reading a program easier than a variable named "Z". As the name indicates, variables are values whose value can be changed. In contrast, a constant is a value whose value does not change, for example the location of a configuration file. The same convention should be used when naming constants as variables, however the use of capitals is a good convention.

Variables may be of different types. The basic data types are integers (whole numbers), real numbers (fractional values), boleean values (true or false) and char (a single character), string (an array of char). Pascal uses strong types, meaning that mixing between different types is prohibited. Other programming languages use weak-typing meaning that a conversion to or from different types are required when mixed. Pascal also has static types, when means that type-checking is performed as part of compilation, rather than during execution. The alternative is dynamic typing - which surrenders safety for flexibility. When it comes to type, Pascal is strong, static and safe.

The following program illustrates the use of variables, constants and types.


program Circum;
const PI = 3.14149;
var diameter, circumference : real;
begin
Writeln('Enter the diameter of the circle');
Readln(diameter);
circumference := PI * diameter;
Writeln('The circle has a circumference of ', circumference)
end.


There are a number of things to notice about this program.

Firstly is the use of = and := for the constant 'PI' and the use of the variable 'circumference', respectively. In the first case the value of PI is equal to the value given. In the latter case, called an assignment operator, the variable on the left-hand side "becomes equal to" the value given on the right hand side.

Secondly, the constants and variables in the program are declared and the type of variables is stated.

Finally, there is basic input and output through the readln and writeln statements. Content from standard input (from keyboard to screen) is read by the program, stored as a variable, which is then used for a calculation, the result of which is sent as part of a statement to standard output (the screen).

Taken as a whole, this program covers a great deal of some basic features of a Pascal program! The program in compilation and operation can be carried out as follows.


lev@squirrel:~/programming/pascal$ fpc circum.pas
Free Pascal Compiler version 2.4.0-2 [2010/03/06] for i386
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for i386
Compiling circum.pas
Linking circum
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
10 lines compiled, 0.1 sec
lev@squirrel:~/programming/pascal$ ./circum
Enter the diameter of the circle
22
The circle has a circumference of 6.91127800000000E+001

Friday, March 18, 2011

Hello World!


Je n'ai fait celle-ci plus longue que parce que je n'ai pas eu le loisir de la faire plus courte. (I would have written a shorter letter, but I did not have the time.)
Blaise Pascal, Provincial Letters: Letter XVI


It is typical in computer programming courses to begin with the simple example of a program that prints "Hello World!". It is a good enough place to start for our purposes. You can use either the IDE or your favourite text editor for this simple program and save the file as hello.pas. It is recommended that the programs be kept in a sensible place (e.g., ~/programming/pascal) for easy and consistent access.

The following is the content of the program:


program HelloWorld;
begin
writeln('Hello World!');
end.


The first line provides a title for the program. The second is a block for the program's actions, starting with a begin statement and concluding with an end statement. The content of the block consists of a single statement, to write a line that outputs 'Hello World'. Note that the brackets for the writeln statement which enclose the text field and the semi-colon to mark the end of the statement.

At first the program is written in source code, a high-level language comprehensible to humans. This needs to be converted to a machine language which the computer can comprehend. This process of conversion is one of compiling the source code into object code, which then is passed to a linker, which combines one or more object files into an executable binary. The following process is illustrative:


lev@squirrel:~/programming/pascal$ fpc hello.pas
Free Pascal Compiler version 2.4.0-2 [2010/03/06] for i386
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for i386
Compiling hello.pas
Linking hello
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
6 lines compiled, 0.8 sec
lev@squirrel:~/programming/pascal$ ls
hello hello.o hello.pas
lev@squirrel:~/programming/pascal$ ./hello
Hello World


The command fpc is the Free Pascal Compiler. As the output explains the compilation creates an object file and then an executable. The error message is benign, as explained by the Free Pascal knowledge base. A directory listing shows the three files. When executed the hello binary produces the output "Hello World!".

Thursday, March 17, 2011

Introduction and Installation

This 'blog site provides a journal on using Free Pascal and Lazarus, the Delphi-compatible object development suite on a Linux system. It is designed to provide a practical introduction to structured and object-orientated programming with a language that is easy to learn, is free and open-source, and is in widespread use and development.

Pascal has been known since the 1970s as a language particularly good for educational purposes. It teaches people good programming practises which they should continue to use if they need to use other languages. A brief essay by Niklaus Wirth, the inventor of Pascal, explains its history.

In terms of other metrics, Pascal is readable and maintainable, but very powerful. Compared with other languages, Pascal has the advantages of quick compilation, fast executables, and low memory consumption. Free Pascal has excellent assembler integration for those who are super-programmers and highly portable, following the principles of "write once, compile anywhere (WOCA)".

There is no other language that can claim all these features. It is surprising, in my opinion, that Pascal is not the language of choice for most programmers.

This journal will begin with installation on Intel/i386 or AMD64/x86_64 CPUs. The most simple method of installation is package based, either through RedHat packages (rpms) or Debian packages (debs). Or one can install from source. The commands for the latter version are as follows:


root@squirrel:~# mkdir -p /usr/local/src/freepascal
root@squirrel:~# cd /usr/local/src/freepascal
root@squirrel:/usr/local/src/freepascal# wget ftp://ftp.freepascal.org/pub/fpc/dist/2.4.2/i386-linux/fpc-2.4.2.i386-linux.tar
root@squirrel:/usr/local/src/freepascal# tar xvf fpc-2.4.2.i386-linux.tar
root@squirrel:/usr/local/src/freepascal# ./install.sh


The shell script will provide all the options. The only variation from the default that I recommend is the installation path, which I used /usr/local/freepascal/2.4.2. To test to ensure installation simply type fp from the console. A text-based integrated development environment (IDE) should execute. Congratulations, you're on you're way towards programming in Pascal!