-
October 28th, 2003, 08:09 PM
#1
Member
Pascal/Delphi Programming Tutorial
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.
\"Death is more universal than life; everyone dies but not everyone lives.\"
A. Sachs
-
October 28th, 2003, 09:51 PM
#2
good first tut, covers the basics of pascal/delphi pretty well...just two things, you didnt give many examples and i found it a little hard to understand at times (and i know pascal), next one maybe a little longer? good first tho.....
slick
\"Look, Doc, I spent last Tuesday watching fibers on my carpet. And the whole time I was watching my carpet, I was worrying that I, I might vomit. And the whole time, I was thinking, \"I\'m a grown man. I should know what goes on my head.\" And the more I thought about it... the more I realized that I should just blow my brains out and end it all. But then I thought, well, if I thought more about blowing my brains out... I start worrying about what that was going to do to my goddamn carpet. Okay, so, ah-he, that was a GOOD day, Doc. And, and I just want you to give me some pills and let me get on with my life. \" -Roy Waller
-
October 28th, 2003, 11:05 PM
#3
Junior Member
Very nice tutorial thanks for taking the time
-
October 29th, 2003, 12:02 AM
#4
Member
First of all thanks for the replies guys, and to try and improve this tut:
Using what we have covered:
Following is a short program written for Pascal that will use some of the things we’ve covered (plus some others, i.e. maths :-O )
Code:
Program UseIt;
{Made by da’dodo}
{This program will take an input and will work out if it is higher or lower than 10, and will write out the factorials of it if lower,
else will write that many dots on the screen}
Uses
CRT; {As before, the CRT external module}
Var {Under this section we’re declaring the global variables}
intInput : integer; {intInput is and integer variable (ie whole numbers)}
chrQuit : char; {chrQuit is a character. It can hold only one character, so no essays!!}
Procedure proWriteDots; {Declare the procedure and give it a name}
{Writes the amount of dots held in variable intInput}
Var {This is where you declare the local variables}
i : integer;
intCurrent : integer;
Begin {The start of the instructions}
For i := 1 to intInput {This loops the commands below up to intInput times – the value of the number the user types in at runtime}
do
begin {The begin allows you to perform multiple instructions within certain functions, such as a for loop or an if statement}
Write(‘.’); {Writes a dot, one after the other}
end {Ends the begin}
; {Ends the for}
End; {Ends the procedure}
Function fncFactorals {The name of the function, called below}(no : integer {A variable that is passed to the function}) : integer
{and the value that is returned}
; {Next thing is this function}
Var
i : integer; {Notice that I’ve used i again. This is commonly used in for loops}
count : integer;
Begin
count := 1; {Initialise the variable count – this sets the starting value, so that an error does not occur later on}
For i := 1 to no
do
count := count * I
{This is the math I was talking about. Basically it multiplies whatever the value in count was by the number of times the program
has been through the for loop}
;
fncFactorals := count; {This assigns the final value to the funtion}
End;
Begin {The start of the main program}
Repeat {Repeats what follows if the condition at the end is met}
ClrScr; {Clears the screen}
Writeln(‘Type in a number’); {Writeln prints stuff to the screen}
Readln(intInput); {This is where the user inputs their value into the program}
If intInput > 10 {Determines if the value is greater than ten}
Then
Begin {And if it is}
Writeln(‘The number is bigger than ten, so here’s some dots!!’); {Write this}
proWriteDots; {And call this procedure}
end
else {If it isn’t greater than 10 }
Begin
Writeln(‘The number is less than 10. The factorials are : ‘); {Write this}
Writeln(fncFactorials(intInput)); {And then write the value this function returns}
end
;
Writeln(‘Do you want to quit?’);
Readln(chrQuit); {Ask the user if they want to quit}
Until chrQuit = ‘y’; {And if they do, don’t repeat the loop}
End.
So here’s a step by step to show how it works:
1) First it starts up and finds all the procedures and functions you have entered. It ignores what these do for the moment, but just notes they’re there
2) Then it goes to the main program. It writes some stuff to the screen and asks for an input.
3) When you hit enter, it passes this value through an if statement, to see if it is greater than 10.
a. If it is greater than 10, it calls the procedure proWriteDots. This then loads the variables into memory and writes out a series of dots, the amount of which will be determined by the input you gave.
b. If it is less than 10, it writes the value of function fncFactorals. It does this by passing a value to it (in the brackets) and the function then performs a series of calculations on it, passing the end value back.
4) After it has done one of these two things it asks if the user wants to quit, and if they do, it finishes the loop and exits the program.
And that's pretty much everything we've covered. If there's something specific that anyone is unsure of, just yell and I'll go into more detail/try to explain it better.
\"Death is more universal than life; everyone dies but not everyone lives.\"
A. Sachs
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|