Pascal By: Liane Tom Outline o Background o Data types and Syntax o Procedures and Functions o Advantages o Disadvantages.

Download Report

Transcript Pascal By: Liane Tom Outline o Background o Data types and Syntax o Procedures and Functions o Advantages o Disadvantages.

Pascal
By: Liane Tom
Outline
o Background
o Data types and Syntax
o Procedures and Functions
o Advantages
o Disadvantages
Background
o Named after mathematician and
philosopher Blaise Pascal.
o Developed by Niklaus Wirth
o Based on ALGOL
o Designed to be a block-structured
language for general use
o It is a compiled language
o Developing and testing programs might
take longer than other languages.
o Designed for teaching programming
to computer science, engineering,
and science students
o Used for undergraduate courses until the
late 1990s
Data Types and Syntax
o Reserved words
o AND, ARRAY, BEGIN, CASE, CONST, DIV, DO, etc.
o Predefined identifiers
o Programmer can change meaning
o boolean, char, integer, real, text, false, etc.
o Ordinal Data Types
o Integer, char, and boolean
o Data are countable, each has a unique
predecessor and successor, and they can be
ordered and compared
o User-Defined Types
o Frequently referred to as enumerated types
o TYPE
Weekday = (Mon, Tues, Wed, Thur, Fri);
VAR
Day : Weekday;
Data Types and Syntax Cont’d
o String constants
o
o
o
o
If a word of one or more characters is used.
Defined in the CONST portion of the declaration section.
Entire string must be enclosed in single quotation marks.
Ex.
CONST
Name = ‘Liane Tom’;
Date = ‘May 1, 2006’;
o Array
o Two methods to declare
o VAR
List : ARRAY[1..5] OF integer;
o TYPE
Numbers = ARRAY[1..5] OF integer;
VAR
List : Numbers;
Data Types and Syntax Cont’d
o Records
o A collection of fields that may be treated as a whole or
individually
o Ex.
VAR
Customer : RECORD
Name : Array [1..30] OF char;
Age : integer;
AnnualIncome : real;
END;
Data Types and Syntax Cont’d
o Sets
o Structured data type consisting of a collection of
distinct elements from an ordinal base type
o TYPE
type name = SET OF base type;
VAR
variable name = type name;
o Ex.
TYPE
Alphabet = SET OF ‘A’ . . ‘Z’;
VAR
Vowels, Consonants : Alphabet;
o Once a set variable is declared, it is undefined
until an assignment of values is made
o Ex.
Vowels := [‘A’,’E’,’I’,’O’,’U’];
Data Types and Syntax Cont’d
o Program
Procedures and Functions
o Procedures
o Procedure heading instead of program heading.
o PROCEDURE procedure name(parameter list);
o END is followed by a semicolon instead of a period
o Declared in the VAR section in the main program
o Procedure heading is also an identifier, so variable identifiers
cannot use the same names if they are on the same level.
o Ex.
PROGRAM Demo2 (output);
VAR
Prac : integer;
PROCEDURE Prac (A : integer);
o Functions
o FUNCTION function name(parameter list) : return type;
o Some value must be assigned to the function name in the
executable section of the function
o Cube := X*X*X;
o Function name cannot be used on the right side of an
assignment statement within the function.
o Cube := Cube + 1;
o In the calling program, the function name cannot be used on
the left of an assignment statement.
Advantages
o Simple and expressive
o Program structure of the main
program, procedures, and functions
are all similar
o Heading
o Declaration section
o Execution section
o No restriction on the length of
variable, function, or procedure
names
o Strongly-typed language so variables
can only be assigned to the correct
data type
Disadvantages
o Compiled language – takes more
time to develop and test small Pascal
programs
o Can overload variables names
between main program and
procedures
o Not that much flexibility since it is
strongly-typed
o No static variables – no variable can
be private to another routine and
retain its value from one call of the
routine to the next
Resources
o Kernighan, Brian W., “Why Pascal is Not My Favorite
Programming Language”. 2 Apr 1981. University of
Virginia, Department of Computer Science. 11 Apr
2006. <http://www.cs.virginia.edu/~evans/cs655S00/readings/bwk-on-pascal.html>.
o “Modula-2”. 8 Apr 2006. Wikipedia. 11 Apr 2006.
<http://en.wikipedia.org/wiki/Modula-2>.
o Nance, Douglas W., Fundamentals of Pascal:
Understanding Programming and Problem Solving.
West Publishing Company, 1986.
o “Pascal Programming Language”. 11 Apr 2006.
Wikipedia. 11 Apr 2006.
<http://en.wikipedia.org/wiki/Pascal_programming_l
anguage>.
o Sebesta, Robert W., Concepts of Programming
Languages. 7th ed. Pearson Education, Inc., 2006.