Day 2
This is the day 2 page for computer programming course 101, an introductions to computer programming. Day 2 introduces the Free Pascal compiler and its basic usage and language syntax.Below are the topics and course materials we are learning today.
Dive into programming with Free Pascal
Free Pacsal is a free open source language and compiler developed by Florian Klämpfl at the University of Erlangen in Nuremberg Germany. It is a community driven project by many individuals across the Internet. It was originally designed as a free open source implementation of Borland's very successful Turbo Pascal product and its natural successor Delphi.The Free Pascal mantra is "write once, compile anywhere", referring to a design where programs written with it can be written one time, and compiled on other platforms resulting in the same results regardless of operating system target.
The Lazarus IDE is a separate project from Free Pascal, but they are often bundled together. Lazarus include many tools and features allowing programmings to create and maintain larger programming projects. It is largely inspired by the Delphi IDE, and commercial product, now sold and marketed by Embarcadero Technologies.
Describing the compiler and linker
The Free Pascal compiler is a command line program that converts Pascal source code into executable binary program files. Assuming a Pascal program source code file named hello.pas might look like the listing below.program Hello; begin WriteLn('Hello World!'); end.The above file could be compiled, linked, and run using these command in a terminal window.
$ fpc hello.pas Free Pascal Compiler version 3.3.1 [2018/09/28] for x86_64 Copyright (c) 1993-2018 by Florian Klaempfl and others Target OS: Linux for x86-64 Compiling hello.pas Linking hello 5 lines compiled, 0.1 sec $ ./hello Hello World!If you were to take a directory listing after compilation you would see the following files.
$ ls
hello hello.o hello.pas
The file colored green is the result of the build process. The hello.pas file is the program source, and the hello.o is an intermediate object file. Object files contain the binary machine code generated by the compilation stage. The compilation stage of the build process is typically followed by a linking stage that links functions required to run a program from the object files to functions provided by external libraries.The result of the build process is usually a binary executable that can be run without further need of the build tools.
Examining the parts of Hello World
If we were to examine the Pascal source code of our hello program, we can learn about a few important principles.`program Hello; begin WriteLn('Hello World!'); end.`
The first non whitespace characters of a Pascal program must start with the program keyword followed by a program name. This block of code is known as the program header. It, like most blocks in Pascal, is terminated with a semicolon ; symbol.
The next non whitespace characters our hello program starts with the begin keyword. The begin keyword marks the start of a block of statements that the program executes first.
File file and the statement block finish with the end keyword followed directly by a period . symbol. This special sequence tells the Pascal compiler it has reached the end of the file, and the sequence is required to appear at the end of every Pascal source code file you will ever write or use.
Programming statement can be placed between the begin and end keywords. In our example the we have the statement WriteLn('Hello World!') terminated by a semicolon ;. This terminating symbol is required at the end of each statement and denotes its end. The WriteLn identifier is the name of the function we are using to print our text output.
Keywords, identifiers, symbols
Free Pascal, like most programming languages, interprets the text of you source code into several distinct categories. At the most fundamental level, the compiler sees your text are one of five kinds of tokens: keywords, identifiers, symbols, numbers, and strings.Keywords, also known as reserved words, are a special sequence of the alphabetic characters, or those in the range of 'A'..'Z', 'a'..'z'. When using an IDE the keywords will typically be highlighted in bold as you type them. In our program the keywords we used were program, begin, and end. Keywords are reserved names which cannot be reused other than than for their intended purposes. Here is a list of all the keywords Pascal reserves.
and
array
as
asm
begin
case
class
const
constructor
destructor
dispinterface
div
do
downto
else
end
except
exports
file
finalization
finally
for
function
goto
if
implementation
in
inherited
initialization
inline
interface
is
label
library
mod
nil
not
object
of
or
packed
procedure
program
property
raise
record
repeat
resourcestring
set
shl
shr
string
then
threadvar
to
try
type
unit
until
uses
var
while
with
xor
Identifiers are names used to reference variables, types, functions, and other bits of code. Identifiers can be of any length, but they must begin with an alphabetic character or _ an underscore character and cannot contain spaces or symbols. Digits, or number characters, are permitted in identifiers so long as they appear after an initial alphabetic character. In our program the identifiers we used were Hello and WriteLn.Symbols are all the non alphabetic and non digit characters which appear in out program. Symbols include characters such as +-/*[]()%<>$@:;., and may have special meaning to the compiler dependent upon the surrounding context in which they are used.
You may have noticed that the single quote ' character was absent from the lit of symbols above. This is because the ' character has a very special meaning. It marks the both the start and end of a character string. A character string, or string for short, is a sequence of literal text embedded into your program. Any characters can be used withing a string with the following caveats. A string must start and end with ' character, and a string cannot span multiple lines.
The final token kind interpreted by the compiler is numbers. Integer numbers can include be written using any sequence of characters between 0..9, and can be written in hexadecimal if they are prepended with the dollar $. For example decimal number 255 could be written as the hex number $FF. Floating point numbers can be written with digits, but include a . dot. For example the floating point value of 1/4 could be written as 0.25.
A few words on case sensitivity
Consider the following two Pascal program listings.program Greeting; var Name: string; begin WriteLn('Hello, what is your name?'); ReadLn(Name); WriteLn('It is nice to meet you ', Name); end.... and ...
Program greeting; Var NAME : String; Begin WriteLn( 'Hello, what is your name?'); READLN(Name); writeln( 'It is nice to meet you ', NAME); END.Both programs compile and will run identically, but they are different. If you examine them you will see that the character case in the two listing is different. Pascal is case insensitive. This means that it does not distinguish a difference between in keywords or identifiers if the character case changes. Most computer programming languages are case sensitive, meaning that WriteLn and writeln would be seen as different identifiers and thus different functions.
Although you have the freedom in Pascal to use any case you want, I would strong encourage you to consistently adhere to writing code as if you were working in a case sensitive language, and also following the case style given in the first example.
PascalCase and camelCase casing styles
With respect to case sensitivity and naming identifiers, there are two main styles used across several programming languages. They are named PascalCase and camelCase. The first style PacalCase uses a convention where each word within an identifier is capitalized. Here are a few examples of PascalCase.WriteLn FileOpen OpenPictureDialog ApplicationThe camellCase style popular with Java looks like this.
writeLn fileOpen openPictureDialog applicationWhen writing programs in Pascal you should strive to always use PascalCase. There are few exceptions, such as with enumeration and set names which we will cover later.
Comments and compiler directives
Free Pascal like all other languages allows you to place comments into your source code. Comments are treated by the compiler as whitespace and are ignored, but you may use comments to document code you write. They may be useful in helping remind you what some piece of code does, how to use it, or embed notes as you work. They may also be helpful to other people that are reusing the code you've written.Here is an example of a program with comments.
{ A program to find prime numbers } program PrimeNumbers; { The IsPrime function tests if a number is prime } function IsPrime(Number: Integer): Boolean; var Max: Double; I: Integer; begin { If the number is 2 then it is a prime } if Number = 2 then Exit(True); { Eliminate even and negative numbers } if (Number <= 1) or (Number mod 2 = 0) then Exit(False); { Start with the divisor of 3 } I := 3; { And go to at most the square root of number } Max := Sqrt(Number); while I <= Max do { If the number divides evenly then it is not prime } if Number mod I = 0 then Exit(False) else { Try the next odd divisor } I := I + 2; { If the code reaches here then we have a prime number } Result := True; end; var Range: Integer; I: Integer; begin WriteLn('Calculate the prime numbers between 1 and what number?'); { TODO: Handle the error created if the value we read in is not a valid number } ReadLn(Range); { Test and display prime numbers between 1 and Range } for I := 1 to Range do if IsPrime(I) then Write(I, ' '); end.Pascal has three different styles of comments. Two block style comments and one single line comment style. Block comments begin and end with { } curly braces and (* *) symbols. Single line comments begin with double forward slashes // and end with the end of line marker.
A special type of comment serves as a directive to the compiler. Curly brace comments that begin with a dollar $ symbol are compiler directives. For example you can tell the compiler to use Intel assembler syntax in a source code file using this directive.
program Divide; {$asmmode intel} // Use Intel assembler syntax { The DivideTwo function is a pure assembler function } function DivideTwo(Number: Integer): Integer; assembler; asm MOV EAX, EDI; // Move argument to register A SHR EAX, 1; // Shift register A one bit right end; var I: Integer; begin for I := 0 to 10 do Write(DivideTwo(I), ' '); end.A list of all possible Free Pascal compiler directives is available in the official compiler documentation here.
Syntax and the trailing semicolon
Often when writing programs you will want to to test your work. The compiler will check the syntax of you source code before it can be compiled. If you've made a mistake, the compiler will do a good job of telling you where the problem is, and what it thinks you've done wrong.When programming with the Lazarus IDE, it will often tell you about errors before you try to compile. These problems will show as red squiggles underneath portions of your code. You can hover the mouse over the squiggles to display a tooltip hint describing the problem. Sometimes a problem cannot be identified until the compiler checks your work, either through a syntax check or build command.
One of the more common mistakes beginners make is either forgetting to put a semicolon ; at the end of a block or statement, or putting a semicolon in the wrong place. When you forget a semicolon, the compiler will tell you it expected a semicolon and place place your cursor near the place in your source code it expected to find it.
Variables, program blocks, statements
The lines of your source that result in executable code depend mostly on variables, programming blocks, and statements. As your programs grow you'll have many of these items in one or more source code files.Variables are universal across all programming languages. It is a storage location a associated with an identifier, containing information referred to as a value. In Pascal variables also have a type such as string or number. When read or writing values to a variable the both the identifier name and type must match. Consider the following block of code.
{ Declare some variables using the keyword var } var Name: string; Age: Integer; begin WriteLn('What is your name?'); ReadLn(Name); // OK, ReadLn can handle strings WriteLn('What is your age?'); ReadLn(Age); // OK, ReadLn can also handle numbers if Name = Age then // Error, Name and Age are different types WriteLn('Your name and age are the same'); if Name = IntToStr(Age) then // OK, Name was converted to a string WriteLn('Your name and age are the same'); end;
In the code above we start by a variable declaration block using the var keyword. Two variables are declared, one of type string, and another of type integer. Note that each declaration is terminated by a semicolon ; symbol. The variable declaration block continues until the next keyword is found.
Individual variables are declared by created a valid identifier name, follow by a color : symbol, and finally a type name. Note that although string is a keyword, it is a keyword for the special string type, and thus does not terminate then variable declaration block.
The keyboard begin marks the start of a programming block which may contain zero or more statements. Not that there is no semicolon after the begin keyword. This is because the semicolon ; symbol is used to mark then end of a declaration, statement, or block. The program block starting with begin will always have a match end keyword, and it is that matching end keyword that has the semicolon ;.
Between the begin and end keywords may come zero or more statements. Statements are the pieces of code which are converted into machine instructions. Every statement must be terminated with a semicolon ;. The many statement types will be covered in the Day 3 lesson.
The extreme importance of identifier naming
In the opinion of the author, choosing appropriate names for identifiers is one of the most important tasks you'll be concerned with when writing software. As a software programmer you be expected to memorize thousands of function names, type names, variables, and more. As such, it is vitally important for you follow easy recognizable patterns when picking identifier names.Here are a few examples of well chosen identifier names.
ShoppingList SaveToFile LoadFromStream CreateDocument ApplyUpdatesHere are a few examples of poor identifier names.
TheList DoIt LoadStuff CrDoc ApplUp