An Intro to Pascal - Basic Pascal / Delphi Programming
By Da’Dodo


Introduction
Pascal and Delphi are two different ‘languages’, which utilise structured programming to great effect, using procedures, functions and modules.
The basic layout of a program is quite simple, although quite picky – ideal for those starting to program, or wanting to branch out into languages other than *cough* vb,
or basic. If you compare these languages with many other “power” languages (i.e. C++, Java) you will find many common features, shared in variable declaration, assignment and
structure.

But before I go into detail with the actual programming of each language, I feel that I should explain why I grouped Pascal and Delphi in one tutorial – I have done this because
they are the same language (*gasp*). Yes I know that is impossible, although really, Delphi is more of a Pascal Two – The Lost Buttons. In other words it uses a GUI (Graphical
User Interface) to enable end users to easily interact with the program.

What you will need to program with
Your ability to produce GUI or CLI (Command Line Interface) programs depends on the compiler which you have – obviously the Pascal compiler only produces CLI programs,
whereas the Delphi compiler produces both. What you also need to factor in is the amount you want to pay for the compiler, and what your bandwidth is.

Delphi compiler
You can buy fully fledged Delphi compilers from… err… shops. And the Borland Delphi website. Alternatively you could download a demo version. You can also get a Delphi type
compiler called Kylix, which is basically the same but for Linux (yey!!).

Order Full Delphi http://shop.borland.com
Download Trial ware http://www.borland.com/products/down...ad_delphi.html (168mb)

Download Kylix (haven’t tried it though, so can’t give an opinion…) http://www.borland.com/products/down...oad_kylix.html

Pascal Compiler
As opposed to Delphi, you can easily download and install Pascal from just about anywhere. It’s free and small (only about 2mb) but you can’t make any pretty GUI apps with it :-(

Borland Turbo Pascal http://community.borland.com/article...20803/tp55.zip
(You may have guessed by now that I quite like Borland products :-D )


Now that you’ve hopefully got a version of Pascal, we can start with some of the basics

Program 1: Hello World!!
The first program, has to be Hello World. I’m sorry but it’s traditional. *shrugs*

Pascal:
Open TP (Turbo Pascal) up, and go to File New. You should now see a nice blue screen ready to go. This is good.
Type the following in

Code:
Program HelloWorld;
{Made by <insert your name here, I’ll let you take the credit for this one!!>}
Uses
CRT;

Begin
ClrScr;
Writeln(‘Hello World!!’);
Readln;

End.
Now after doing that, save it and goto compile. Work through any typos (later on it can be a bit more severe, but this is a fairly mundane program!!), and then goto run.

Now you should have a black screen that says “Hello World” on it. To exit hit enter.
Now lets pick it apart

Program HelloWorld; - This gives your program a name, and has to be there. Note ;
{Made by <insert your name here, I’ll let you take the credit for this one!!>} A comment. Note the { and }
UsesThis is where you declare the external modules that your program is going to use
CRT; I usually use this module in everything. It allows for manipulation of the screen to a degree that would be otherwise impossible. Again note the ;

Begin This is where the instructions of the program start. Every Begin must have an End with it though!
ClrScr; This command clears the screen. Try taking it out and running your program again. Notice the difference!!?
Writeln(‘Hello World!!’); Writes a line of text to the screen. More on this later
Readln;Reads a line of text in. It is one way of preventing the program continuing until the user is ready

End. The end of the instructions. If it is the end of the program it has a . by it

Delphi:
Open Delphi, and go to File New. Select new executable and hit ok. You should then see a blank form ready to go.
Select the form by clicking it and click on the events tab (in the properties window). Double click the text field next to “OnActivate” and type the following in:

Code:
Form1.caption := ‘Hello World’;
Now hit F9 (assuming they haven’t changed the shortcut) or click on Run. You should see a blank form with the title “Hello World”. Click on the X to close it.

As you can see, a lot of the background work is done for you, which is always nice, and makes it a lot quicker to program in. However, this is a double sided sword, because you
don’t get to learn about the way procedures are implemented, and how they are composed, making it a lot harder later on for you to write a more advanced program.

Despite this, we have just done something that is vital to programming – variable assignment. You see the little := that’s how you assign a variable a value. This is used
everywhere, in loops, functions, arithmetic, etc, etc. Lets have a closer look at it to find out exactly what is happening.

Form1.caption – Select the variable (if you want to be picky it’s a property) “caption” of the object “Form1”
:= - assign the value following this symbol to the variable selected
‘Hello World’ – The value to be assigned. If it isn’t a variable you need to use single speech marks ( ‘ ) to use it.
; - End of the line of instruction to be carried out.

So there we go.
What we have covered is:

The structure of a program. This applies for both Pascal and Delphi.
Using external modules in Pascal
Variable assignment in Delphi
Executing a program
Basic user control in Pascal
Basic statement syntax

So there you go. That was just an intro. If you like it, feel free to post happy smilies (or alternatively comments, which ever takes your fancy). And if you don’t like it then umm…
send comments to /device/null or Recycle Bin (again depending upon your preference). No seriously, any comments readily received.

Part 2 (which I wrote later, after finding the first part)

Delphi object selection

In Delphi, one of the major things that you will need to do is select object (i.e. a status bar, text box, etc). This is done very easily, and is quite similar to other languages (I think
vb is practically identical, from memory). There are several levels the syntax of selecting an object (and eventually a property) first you need to select the form that it is on. Then
you need to select the actual object, and finally the property. For example

Code:
frmMain.chkPassword.state
  |         |         |  
form      Object   Property 
You include this in a statement and then it is treated as a normal variable, within that particular statement. So in this case you could do:

Code:
frmMain.chkPassword.state := cbChecked;
Note – From now on, unless noted, all syntax is valid for Pascal and Delphi

Iteration and selection

Iteration

Iteration is basically putting your program into a loop. There are several types of loops, but the main ones that I use are the for and until loops, the syntax for which are;

Code:
For I {The variable name, must be declared as an integer} := <start number> to <finish number> 
   Do
    Begin
     <instructions>
    End
;
Code:
Repeat
 <Instruction>
until <variable> = <value> ;
Selection

Selection is just a fancy way of saying if functions. This can be done through if statements, embedded if statements or case statements.
I will cover "if" here, and the others later on… maybe. Very useful in programming (almost impossible to do anything constructive without it), as it allows to take the program
down a different route, depending upon what the user (or sensor, etc) chooses to do.

IF syntax
Code:
If <variable name> = <value>
  Then
    Begin
     <instructions>
    end
   else {Carry’s out some instructions if the variable and value aren’t the same}
    begin
      <Alternative instructions>
    end
;
Functions and Procedures, and comments

One of the most important things in modern programming is structure (and therefore readability). This is achieved at a basic level through procedures, functions and comments.

Comments

In Pascal the easiest way to write a comment is between { and }. This can be multi-lined, and will comment everything between the clause brackets.
In Delphi, I tend to use // as it is a lot easier if you only want to write a single line (which is usually what you want to do).

Procedures

The structure of a basic procedure is the same for both languages;

Code:
Procedure <procedure name> ;
Uses
<Like with the hello program!! – Any external modules used>
Var
<Declare any local variables that can only be used within that procedure>
Begin
<instructions>
End;
If you’re using Delphi, and you want an action to be performed when the user does something, just goto the Events tab in the properties window and double click the action
you want to run the procedure. This will write out the skeleton of a new procedure, all ready for you to edit!!

Functions

Functions are a bit harder to write. The first thing you have to get your head round is that they return a value. The second, is that most of the time you’ll want to pass a
variable to them. This is done by;

Code:
Function fncExample(var1 : string; var2 : integer {Declare your passed over variables}) : string; {Declare what type of variable the function will return}
Var
<declare local variables here>
Begin
  <Instructions>
  fncExample := strFinalOutput ; {Note – you must assign the function a value at some point!!
Just remember you can’t use the value of the function in a calculation, within the same function – this would cause a loop back } 

End;
Calling procedures and functions

The one thing you have to remember when calling a procedure or function, is that the procedure/function you are calling must be before the command to call it.

To call a procedure is very very easy- just type the name, followed by ; i.e. proExample;

To call a function is a bit harder, as you may have to enter some variables. Most of the time you will want to assign the result of the function to a variable, which can then be
manipulated or used in an ‘if’ function.

Code:
intVariable := fncExample(var1,var2,var3);
Looking back on it, it wasn’t really that hard was it?


So in the second part we have covered:
- Object selection
- Variable passing
- Basic procedures
- Functions
- Selection
- Iteration
- Calling procedures, functions

Glossary

Form – The canvas of a program – when you start a new program in Delphi this is what you get to place all your objects on.

Object – Something which is placed on a form. An example of an object would be the text box or ok button at the logon screen of windows.

Property – Just what it says. This is an attribute of an object, that you can change, to alter the behaviour of it.

Iteration – Repeating things (Polly the parrot style)

Selection – Deciding on an outcome based on the input

Variable passing – When you pass a variable to a function for use within that function. Could alternatively be done with global variables, but could get very messy very quick.

If you’ve got down to here then well done (for reading it all that is!). There could be another one covering file access and some other stuff (like basic music) if you want it, any
suggestions for other material is welcome. As I said before, any questions/constructive comments welcome.