HOME - - - - - - - - - Other material for programmers - - - - - - - - - Pascal Tutorials Table of Contents

Pascal Programming: First Steps, using the opensource FPC, aka Free Pascal



N.B. This version of the first tutorial is tailored to the needs of readers who wish to learn about Pascal using FPC Pascal.

Alternate versions of the tutorial are available tailored to the needs of others-

First steps with Pascal using Borland's Turbo Pascal, version 5.5, which is available for free download.

First steps with Pascal using the free Pascalite compiler.



"Let's start at the very beginning...."

A ten-year-old working at home without help should be writing programs in Pascal, using FPC (Free Pascal) after applying him or herself to the following... if I've done it right! For those of you who are NOT new to programming, may I ask you to skim through the "Level One" material anyway? There are some things you might find useful. I'm working on a Windows 98 machine. Windows 95 or the higher Windows should also work.

"First catch your rabbit", in this case download and install the free software. I know: Its a hassle. It will take disc space. There are risks. Well, FBC installed for me with minimal hassle. As the man said... Try it, you (may) like it. And at least in this case, you have some reassurance that it "should" work.

Everyone: Please make use of the good helpfiles that come with your Pascal! My tutorials are not meant to supplant those, merely to take you through what is there in an easy (I hope) sequence. A useful feature of FPC is that if you are editing your program, and you want to look up say, GetTime, in the compiler's helpfile you are supposed to be able to simply type "GetTime" (without the quotes), leave the cursor in the word, or just past it, press F1, and, hey presto, you'll be taken to the right entry in the help file! To get this working entails an extra install step, and I have to confess that I seem to have fluffed it... to date!

Already, I have a working "help files" answer. In accordance with the instructions that arise if you press F1 within FPC, I downloaded the zip archive with the help files. I unzipped it. (You must allow your unzipper to put the files in the sub-folders they are meant to go in. Don't just "explore" the archive, and try to drag everything into a single folder. On a Win98 machine, with WinZip, it all goes smoothly.) When you've done this, you can access the help files with any internet browser. Simply open the fpctoc.html page. To integrate the help files with the compiler is quite simple... I think... but I've done something wrong. All you have to do, supposedly, is access (within FPC, from the menu) "Help | Files..." and tell FPC where to find fpctoc.html. As I said, my installation isn't working (yet!) I'm pretty sure that my "problem" is just that I didn't put the help files in the same folder as the FCP exec file. I didn't want to co-mingle the help files and the compiler, but it may be necessary. The instructions tell you to do this. There are no name overlaps, anyway, which is good.

Launch your Pascal. With FPC Pascal, you get an MS-DOS command prompt window "promptly" (heh! heh! (Sorry)), and it then looks as if something's wrong, or as if the computer is waiting for you to enter some command. Be patient! After what will seem an age until you are used to it, "stuff" will appear in the window, not least a menu bar and an edit window. Don't worry... once "awake" it runs as fast as it should. (On my reasonably fast (for it's vintage) Win98 machine, it just took 40 seconds.)

Once your Pascal is running, and you are in the edit window, enter the following:

program first;
uses crt;
begin
ClrScr;
writeln('Hi');
end.

Some of the text that you've typed in will appear in different colors. More on that later; more on what's going on later, but for now: Let's make something happen! But before we do, let's save your typing.

Click File|Save. This will actually bring up the "Save As" dialog box, because this is the first time you are saving the program you've just typed in. You could just enter "My Program" in the file name box and click "ok", but I'm going to suggest a slight improvement.

You may be in the folder (directory) into which you installed FPC. Or someplace else... FPC seems to save where you were from invocation to invocation. I would suggest making a folder for the files you create. This isn't as easy in FPC as one would hope... but it is do-able, if a little strange to Windows-spoilt users. Use your tool of choice- I like Windows Explorer- to create, outside of FPC, any folders you want to use.

Once the folder is created, to move into that folder. Navigating the tree of folders is a little strange at first, but you can do it! Big help: The "file" called "..\" is a link to the parent folder of whatever folder you are in. Assign a name for the file, I'd suggest "First", or "PLT1aff". (PascaL Tutorial 1aff, which is the name of the web page you are reading.) Click "Save".

Do, by the way, remember to drag the lower right hand corner of the FPC box outwards. You can't resize the window quite as freely as you could most other Windows windows, but you can at least give yourself a larger font.

(A little aside for people very new to Windows. The following is true not only for FPC, but for most Windows applications. If you've started something from scratch, i.e. by using File|New, then the first time you save it, you get the "Save As" dialog, as I've already said. From that point on, if you use File|Save, your work is simply saved where ever, and under whatever name you specified during the first save. The previous version of your work is over-written. Also, when you load something pre-existing, then doing File|Save will again simply replace the previous version. If you have been working on something, and you don't want to destroy the previous version, then use File|Save As, and change the name used for saving what you have.)

Keyboard shortcuts, like F2 for save, are always nice. Try to start using a few; the practice may grow on you. Ctrl-F9 to compile and run your code, and F6 to switch between windows are particularly useful.

Several common Windows keyboard shortcuts, notably ctrl-C / ctrl-X and ctrl-V don't do the same thing in FPC as they do in Windows. In FPC you use ctrl-insert, shift-delete and shift-insert to accomplish copy/ cut/ paste. (By the way: I once had a weird disruption of copy/ paste in my Windows application while FPC was open in another window. I only had to close and restart FPC to resolve the problem.)

Selecting text works a little differently in FPC than in Windows, and selected text is not automatically deleted if you go to type something on top of it. If you get some selection that you want to get rid of, just do shift-UpArrow followed by shift-DownArrow.

So... you've typed in the little program I gave you. You've saved your typing.

Now click "Run" on the menu, and "Run" again from the sub-menu (or just press ctrl-F9, the equivalent... as you can see from the right-hand column of the sub-menu.) You may well get an error message, in which case you'll get messages, and, probably the words "Compile Failed". Your Pascal will even tell you where it got confused... but that isn't always where the problem lies. If you can't see a fault where your Pascal says to look, check preceding lines. One favorite trick of new Pascal programmers: Leaving out a semicolon. (Although, in rare cases (which you'll learn to be aware of), a superfluous semicolon may be the problem!) The error message even tells you which line of your program holds the place Pascal got confused.

When there's a message on the screen, you tell it "Okay, got that, now go away" by clicking on the little square in the upper left of the message's box, or pressing by escape or enter.

The interface used with FPC is from the days before Windows. (Yes, there was such a time... When I was a boy... (Don't let me get started!!))

FPC does work in a "windowed" environment, but you could be forgiven for not liking it. However the concepts of maximized and "restored" windows are present. (I'm not sure how to mimic a minimize.) You can... really... even re-size the windows. It's just all less intuitive than modern Windows.

If you had an error, find it, maybe re-save, and try to run the program again.

Don't be alarmed if it seems that nothing has happened.

When you finally (or if you are a careful copier, the first time you...) ran the program and got no "complaints", you may have noticed the screen flash, and you might even have seen the "Hi" you've accomplished. But it didn't stay. Your sourcecode ("program first; begin...") came back. FPC: Press Alt-F5. TP: Use the menus: Alt-R (To get the "Run" menu, then U for "user screen". That SHOULD bring you to a screen with "Hi" on it... someplace. There may be other "stuff" there, too. I must confess that I had a little trouble with this, so just keep at it... you'll get that User Screen thing eventually! It's been a while since I used TP, and I am a very new user of FPC. I have every confidence that both are usable... it just takes a little struggling to get fluent. I hope these pages are, at least, saving you some of the struggle? With FPC, Alt-F5 seemed to work well for switching back and forth between the sourcecode, in the editor, and the "user screen". As I said, with TP, use the menu to access the User Screen (the menu SAYS the key combination "Alt-F" should do it... but it wouldn't for me, on my XP machine.) And use the escape key to go back to the sourcecode.

Whew! It gets easier. I promise.

Just one last little flourish to finish off this first tutorial. You won't finish a real program in one go. Part of learning to program is learning to build something up by degrees. Add a second writeln line, specifically writeln('Bye'); Compile and run the program again; see your new masterpiece in action (or fix the typos!) and cheer... you're a programmer!


Oh yes... I said I'd take you through the program....

Every Pascal program begins with the word "program". After that, you supply a name; I chose "First" in this case. (PLT1a would have been better, but might have confused you at that stage.) And then the line is finished with a semicolon. You get a lot of semicolons in Pascal. More on them another time.

Next in our simple first program came the word "begin". For every "begin" there is an "end". You can have many begin/ end blocks, and they always nest. Leaving out the rest, a complex program might look like.....

begin
  begin
    begin
    end;
  end;
  begin
    begin
      begin
      end;
      begin
      end;
    end;
  end;
end.

Did I scare you? I hope not... I wanted you to look forward to the excitement of a really sophisticated project! Anyway. Begins are always paired with ends, as I said. You do NOT put a semicolon after the begin (unusual), but you do put one after the end... usually. (I'll discuss the exceptions another time.) After the final end in a program, you put a period (full stop) instead of the semi-colon.

"Clrscr" cleared the screen.

write and writeln are words built into all the Pascals we've been discussing. (A rough equivalent for Delphi, by the way, is "showmessage", e.g. showmessage('Hi');).

Note that we put apostrophes around the text we want written, rather than the quotation marks we would use in everyday English.

And we've already covered the "end.", so we're almost done! In a moment, you can go give yourself a treat.

Or return to the index of Pascal tutorials for another one!

One little thing before we go. You see the way things are indented in the big block of begins/ ends above? You can move text around to set it out any way that helps you see what is going on. Pascal is unfussy about spaces and new-lines. Guess what? "More on this later." (Again!)


Please also note that I have two other sites, and that the following search will not include them. They have their own search buttons.

My Sheepdog Guides site.
My Arunet site.

Go to the sites above, and use their search buttons if you want to search them.
To search this site....

Search this site or the web powered by FreeFind

Site search Web search
The search engine merely looks for the words you type, so....
*    Spell them properly.
*    Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I"....

You can also search this site without using forms.
Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.


Want a site hosted, or email? You can also help me if you sign up via this link to 1&1's services. (I wouldn't recommend them unless I was happy after several years as one of their customers, but yes, they do pay me if you use this link! As do the Google advertisers, about whom I know nothing, of course.)



Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org


Why does this page cause a script to run? Because of the Google panels, and the code for the search button. Also, I have some of my pages' traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try one, check out their site. Why do I mention the script? Be sure you know all you need to about spyware.
Editor's Main Homepage
How to email or write this page's editor, Tom Boyd

....... P a g e . . . E n d s .....