pascal course 234319spring 20141. introduction designed: 1968/9 by niklaus wirth published: 1970...

27
Pasc al Course 234319 Spring 2014 1

Upload: scot-mosley

Post on 27-Dec-2015

229 views

Category:

Documents


1 download

TRANSCRIPT

Pascal

Course 234319Spring 2014 1

Introduction• Designed: 1968/9 by Niklaus Wirth• Published: 1970• Imperative, structural, procedural• Static and strong typing• Static binding• Designed to allow simple way to handle complex

data structures efficiently, and be easy to learn.• We will use:

– FreePascal 2.4.0http://www.freepascal.org/download.var

Spring 2014 Course 234319

These concepts will be explained in the lectures

2

A basic Pascal programprogram HelloWorld;

{ Definitions are placed here - types, variables, procedures, functions, … }

beginWriteLn(‘Hello World!’);{ More statements can be added here }

end.

Winter 2010/11 234319 Course 3

A basic Pascal programprogram HelloWorld;

{ Definitions are placed here - types, variables, procedures, functions, … }

beginWriteLn(‘Hello World!’);{ More statements can be added here }

end.

Winter 2010/11 234319 Course

Program Heading

4

A basic Pascal programprogram HelloWorld;

{ Definitions are placed here - types, variables, procedures, functions, … }

beginWriteLn(‘Hello World!’);{ More statements can be added here }

end.

Winter 2010/11 234319 Course

Block

5

A basic Pascal programprogram HelloWorld;

{ Definitions are placed here - types, variables, procedures, functions, … }

beginWriteLn(‘Hello World!’);{ More statements can be added here }

end.

Winter 2010/11 234319 Course

Declaration Part

6

A basic Pascal programprogram HelloWorld;

{ Definitions are placed here - types, variables, procedures, functions, … }

beginWriteLn(‘Hello World!’);{ More statements can be added here

separated by a semicolon }end.

Winter 2010/11 234319 Course

Statement Part

7

Built-in Data Types• Primitive types:

– integer, boolean, real, char.

Modern implementations supply more:– Longint, shortint, extended, cardinal, byte, word…

• Output:– Write(1, 2, “hello “, “World”);– WriteLn(x, y);

• Input:– Read(x);– ReadLn(y);

Spring 2014 Course 234319 8

Data Types - cont.• We can create our own types:

– Enumerated types: type Color = (Red, Green, Blue, Yellow);

Enumerated types are ordered:Red < Blue = trueord(Yellow) = 3

– Subrange types:type Letter = ‘A’ .. ’Z’; Index = 3 .. 8; BasicColor = Red .. Blue;

– Subset types: type Rainbow = set of Color;

Spring 2014 Course 234319 9

succ(Red) = Greenpred(Blue) = Green

Data Types - cont.– Records (similar to C structs - one of Pascal’s original inventions)

type date = record day : 1 .. 31; month : ( January, February, March,

April, May, June, July,August, September, October,

November, December); year : 1900 .. 2100; end;

– Variant records

Spring 2014 Course 234319 10

Spring 2014 Course 234319 11

Flow Control• If x < 2 then

write(x);

• The expression between the “if” and “then” must be of type Boolean.

• If x < 2 thenbegin write(x);end

• If x < 2 then write(x) // no semicolon!Else write(y);

Spring 2014 Course 234319 12

Flow Control• case i of

1 : write(“A”); 2 : write(“B”); 3 : write(“C”) // no semicolonend

• Case works well with more complicated data types, as we will see.

Spring 2014 Course 234319 13

Flow Control• While x < 5 do

begin read(x);end;

• Repeat read(x); Until x > 5;

• 15: Write(x)Goto 15;

• For i := 1 to 10 do WriteLn(i);

• For i := 10 downto 1 dobegin WriteLn(i);end;

Arrays in Pascal

Spring 2014 Course 234319 14

• Pascal arrays are defined as follow:array [<index-type>] of <element-type>

• Multidimensional arrays: • array [1..5, 8..10] of boolean;

• Example:• var A : array [1..5] of real;• var pens : array [Red..Green] of record

width : 1..3;kind : (Regular, Bold);

end;• For col := Red to Yellow do

writeLn(pens[col].width);

!!!

Functions and Procedures • Pascal functions always return a value

function myFunc(a:integer; b:real) : real;

begin myFunc := a * b; //note how we set the valueend;

– Functions cannot be called as a standalone statement.– In this example, `a` and `b` are passed by-value.

• A function that doesn’t return anything is a procedure.procedure myProc(var a : boolean);

begin WriteLn(“Hello, World”);

a := true; // we change the argument itselfend;

Spring 2014 Course 234319 15

A simple problem…• Given a range of positive numbers:

– Summarize all numbers in range that divide by 3 or 5.– Print the result.

Spring 2014 Course 234319 16

program Sum;function sumOfMatching(s, e : integer) : integer; var sum, i : integer; begin sum := 0; for i := s to e do begin if (i mod 3 = 0) or (i mod 5 = 0) then sum := sum + i; end; sumOfMatching := sum; end;begin WriteLn( sumOfMatching(1,300) );end.

Version 1

Spring 2014 Course 234319 17

Version 1program Sum;function sumOfMatching(s, e : integer) : integer; var sum, i : integer; begin sum := 0; for i := s to e do begin if (i mod 3 = 0) or (i mod 5 = 0) then sum := sum + i; end; sumOfMatching := sum; end;begin WriteLn( sumOfMatching(1,300) );end.

What if s<0? e<0?

AuxiliaryFunction?

Spring 2014 Course 234319 18

Version 2program Sum;type positiveInt = 1..MAXINT;function isMatching(i : integer) : boolean; begin isMatching := (i mod 3 = 0) or (i mod 5 = 0); end;function sumOfMatching(s, e : positiveInt) : integer; var sum, i : integer; begin sum := 0; for i := s to e do begin if isMatching(i) then sum := sum + i; end; sumOfMatching := sum; end;begin WriteLn( sumOfMatching(1,300) ); end.

What if s>e?

Spring 2014 Course 234319 19

Version 3program Sum;type positiveInt = 1..MAXINT;function SumOfMatching(s, e : positiveInt) : Integer; var sum, i : integer; function isMatching(i : integer) : boolean; begin isMatching := (i mod 3 = 0) or (i mod 5 = 0); end; begin sum := 0; for i := s to e do begin if isMatching(i) then sum := sum + i; end; sumOfMatching := sum; end;begin WriteLn( sumOfMatching(1,300) ); end.

Spring 2014 Course 234319 20

Can it be done in C/C++?

Version 4program Sum;type positiveInt = 1..MAXINT;function sumOfMatching(s,e,div1,div2:positiveInt):integer; var sum, i : integer; function isMatching(i , d1, d2 : integer) : boolean; begin isMatching := ((i mod d1=0) or (i mod d2=0)); end; begin sum := 0; for i := s to e do begin if isMatching(i,div1,div2) then sum:=sum+i; end; sumOfMatching := sum; end;begin WriteLn( sumOfMatching(1,300, 3, 5) ); end.

‘div1’ and ‘div2’ are already known to nested

function ‘isMatching’.Spring 2014 Course 234319 21

Version 5program Sum;type positiveInt = 1..MAXINT;function sumOfMatching(s,e,div1,div2:positiveInt):integer; var sum, i : Integer; function isMatching(i : Integer) : boolean; begin isMatching:=(i mod div1=0) or (i mod div2=0); end; begin sum := 0; for i := s to e do begin if isMatching(i) then sum := sum + i; end; sumOfMatching := sum; end;begin WriteLn( sumOfMatching(1,300, 3, 5) ); end.

Spring 2014 Course 234319 22

A more general solution

• We can also change ‘isMatching’ to receive a matcher - a pointer to a function, as an argument, and call it with any integer→boolean function we desire.

Spring 2014 Course 234319 23

program Sum;type positiveInt = 1..MAXINT;type matcher = function ( i:integer ) : boolean;

{ defining a matcher }function m1( i : integer ) : boolean; begin m1 := (i mod 7 = 0) or (i mod 13 = 0); end;...

Spring 2014 Course 234319

Version 6

24

...function sumOfMatching( s, e : positiveInt ; isMatching : matcher ) : integer; var sum, i : Integer; begin sum := 0; for i := s to e do begin

if isMatching(i) then sum := sum + i; end; sumOfMatching := sum; end;

begin WriteLn( sumOfMatching(1, 300, @m1) );end.

Spring 2014 Course 234319

Version 6 – cont.

Notice the syntax – ‘@’Generally “address of”

25

• Multiline:– { … }– {* … *}– (* … *)

• Single line– // …

Spring 2014 Course 234319

Some comments

26

• Exceptions• Generics• Tuples• Foreach loops• Named parameters• Classes…• Coroutines / generators• Closure• Garbage collection• Varags, variable-length arrays

Spring 2014 Course 234319

What’s missing?

27