pascal by: liane tom. outline o background o data types and syntax o procedures and functions o...

12
Pascal By: Liane Tom

Post on 21-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

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

PascalBy: Liane Tom

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

Outlineo Backgroundo Data types and Syntaxo Procedures and Functionso Advantageso Disadvantages

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

Backgroundo Named after mathematician and

philosopher Blaise Pascal.o Developed by Niklaus Wirth

o Based on ALGOLo Designed to be a block-structured

language for general useo 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 studentso Used for undergraduate courses until

the late 1990s

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

Data Types and Syntaxo Reserved words

o AND, ARRAY, BEGIN, CASE, CONST, DIV, DO, etc.

o Predefined identifierso Programmer can change meaningo boolean, char, integer, real, text, false, etc.

o Ordinal Data Typeso Integer, char, and booleano Data are countable, each has a unique

predecessor and successor, and they can be ordered and compared

o User-Defined Typeso Frequently referred to as enumerated typeso TYPE

Weekday = (Mon, Tues, Wed, Thur, Fri);VAR Day : Weekday;

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

Data Types and Syntax Cont’do String constants

o If a word of one or more characters is used.o Defined in the CONST portion of the declaration

section.o Entire string must be enclosed in single quotation

marks.o Ex.

CONSTName = ‘Liane Tom’;Date = ‘May 1, 2006’;

o Arrayo Two methods to declare

o VAR List : ARRAY[1..5] OF integer;

o TYPE Numbers = ARRAY[1..5] OF integer;VAR List : Numbers;

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

Data Types and Syntax Cont’do 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;

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

Data Types and Syntax Cont’do 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’];

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

Data Types and Syntax Cont’do Program

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

Procedures and Functionso Procedures

o Procedure heading instead of program heading.o PROCEDURE procedure name(parameter list);

o END is followed by a semicolon instead of a periodo Declared in the VAR section in the main programo 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 Functionso FUNCTION function name(parameter list) : return

type;o Some value must be assigned to the function name

in the executable section of the functiono 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.

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

Advantageso Simple and expressiveo Program structure of the main

program, procedures, and functions are all similaro Headingo Declaration sectiono 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

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

Disadvantageso 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

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

Resourceso 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/cs655-S00/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_language>.

o Sebesta, Robert W., Concepts of Programming Languages. 7th ed. Pearson Education, Inc., 2006.