pjhayward's Pascal Tutorial |
|
pjhayward.net Home Got a question? Tutorial Home Preparation Lesson One - Sample Program Lesson Two - Program Structure Lesson Three - Data Types and Constants Lesson Four - Variables Lesson Five - Text I/O Lesson Six - Subroutines Lesson Seven - Conditional Statements Lesson Eight - Arrays Lesson Nine - Loops Lesson Ten - Units | Lesson One - "Hello, World!": A Sample Program
Now that you have installed Free Pascal, open up the IDE (Integrated Development Environment). You should have an icon either on your desktop, or in the start menu. If you can't seem to find the shortcut, find where you installed Free Pascal, then open the bin/win32/fp.exe file. (Linux users: run 'fp' in a terminal window) Now that you have the IDE open, we're going to put in a sample program and compile it. The IDE runs in a command prompt, so your mouse won't work. Get used to not using your mouse much when you're coding. Once you move on to Delphi or some other visual development interface, you'll use it often enough. But for now, stick to your keyboard. Menus are opened by pressing the Alt key, along with the highlighted letter in the menu name. So open File, then select New. You can either use your arrow keys, or just press "n". Now, enter the following source code into the IDE: (Everything BETWEEN the start code and end code notes - don't include the lines with the dashes) program hellowld;
begin
writeln('Hello, World!');
readln;
end.
--- End Code ---
Now that you have your program typed in, you should save it. Open the File menu again (Alt-F), then select Save. At the bottom of the save window, you will see the directory (the original name for "Folder" that you are currently working in. If you ran the IDE straight from the install, you will notice that you are in the bin\i386-win32 folder of the Free Pascal installation path. That's generally a bad place to store your source code files. You can use the .. directory (use the Tab key to move between fields) to move up one directory until you get to the base installation folder of Free Pascal. (C:\FPC for the default installation.) This still may not be the best place to put your code, but it's easy to remember. If you know where you want to put your source code, go ahead and store it wherever you want. Once you are in the directory you want, type the name "hellowld.pas" into the Name box (Alt-N, or use tab) and save the file. Once you have saved your source file, open the Run menu (Alt-R), then select Run. At this point, the IDE takes a couple of steps. First, it checks to see if the file has been changed since it was last compiled. If it has, it runs the compiler on the source code file, which translates it into an executable file, or program. This is stored in the same directory as the source code file, unless you have the compiler configured to store it somewhere else. It then runs the compiled program. If no changes were made, it just runs the existing compiled program. At this point, you should see a black console window, with the text "Hello, World!" at the top left. If by chance you get an error, make sure you typed the program exactly as it appears on the web page - make sure you have semicolons at the end of each line (except the "begin" line), and make sure you used single quotes (also known as an apostrophe) around the text "Hello, World!". Pascal is very strict on program structure. This is generally a good thing, because it helps protect you from common mistakes that can result in fatal program crashes. Of course, you can still find plenty of ways to crash your programs, but the strict syntax is helpful. Once you are done admiring your handiwork, press the Enter key to return to the IDE. At this point, you should see the code for the program in the IDE window. I'll go through line by line and explain what each does.
|