The Free Pascal Compiler and Lazarus can be downloaded at no cost at www.freepascal.org. Free Pascal and Lazarus are a Turbo Pascal / Delphi clone that can make programs for Windows, Macintosh (Mac OS X), and Linux.
This tutorial will cover data types. The data types that are most common are the integer, real (floating point), string, char, and boolean. There are several other data types that are built into Free Pascal. The list is found in the documentation on Free Pascal's Website www.freepascal.org. This tutorial will also cover getting user input like names and numbers and then printing the information to the screen.
The common data types that should be known are:
Integer - It is a whole number. Its range varies by system. For example in Windows XP the integer’s range is -2147483648 to 2147483647.
Real - Real is a floating point number. Its range varies by system. For example in Windows XP, real’s range 5.0E - 324 to 1.7E 308.
String - String is text.
Char - Char is treated like a single text character by the computer. For example: a, B, &, @, l, and 4.
Boolean - A boolean variable is either TRUE or FALSE
To create a variable, place the word var between the uses section and begin (program section). The syntax for variable include:
name_of_variable:data_type;
Example:
var
name:string;
num1:integer;
num2:integer;
total:real;
To have the computer receive input from the user the command readln (or its variation read) will collect the data. Also, the variable with the data type must be declared.
Example:
write('Enter Name: ');
readln(name);
The computer will collect the data from the user and this line will output the data to the screen.
writeln('Nice To Meet You, ', name);
Here is a program that shows user input, variables, and output of information that was used in the video tutorial.
program FPTutorial4;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes
{ you can add units after this };
var
name:string;
num1:real;
num2:real;
total:real;
begin
write('Enter Name: ');
readln(name);
write('Enter First Number: ');
readln(num1);
write('Enter Second Number: ');
readln(num2);
total := num1 + num2;
writeln;
writeln('The Sum Of The Numbers Is: ', total:0:2);
writeln;
writeln('Nice To Meet You, ', name);
writeln;
writeln;
writeln('Press <Enter> To Quit');
readln();
end.
Somethings to try on your own:
Get the program to accept your address, phone number and birthdate.
Video: Free Pascal Tutorial 4 - Data Types and User Input - Lazarus (8:21)