23
Pasc Pasc al al 234319 Course Winter 2010/11 1

Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

Embed Size (px)

Citation preview

Page 1: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

PascalPascal

234319 CourseWinter 2010/11 1

Page 2: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

IntroductionIntroduction• Imperative and procedural programming language• Designed: 1968/9• Published: 1970• Static and strong typing• Static binding

• We will use:– FreePascal 2.4.0

http://www.freepascal.org/download.var

Winter 2010/11 234319 Course

These concepts will be explained in the lectures

2

Page 3: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

A basic Pascal programA 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

Page 4: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

A basic Pascal programA 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

Page 5: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

A basic Pascal programA 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

Page 6: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

A basic Pascal programA 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

Page 7: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

A basic Pascal programA 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

Statement Part

7

Page 8: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

Data TypesData Types• Pascal has 4 primitive types:

– integer, boolean, real, char

• We can also create our own types:– Enumerated types:

type Color = (Red, Green, Blue, Yellow);type MonthType = (January, February, ... ,December);

Enumerated types are comparable:Red < Blue = true,succ(Red) = Green,pred(Blue) = Green,ord(Yellow) = 3

Winter 2010/11 234319 Course 8

Page 9: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

Data Types - cont.Data Types - cont.– Subrange types:

type Letter = ‘A’ .. ’Z’; Index = 3 .. 8; ColorList = Red .. Blue;

– Records (Complex types like C structs):

type date = record day : 1 .. 31; month : MonthType; year : 1900 .. 2100; end;

Winter 2010/11 234319 Course 9

Page 10: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

Arrays in PascalArrays in Pascal

Winter 2010/11 234319 Course 10

!!!

Page 11: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

Functions and Procedures Functions and Procedures • Pascal functions always return a value

function myFunc(…) : int;

begin … myFunc := 13; {note how we set the value}

end;

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

begin …end;

Winter 2010/11 234319 Course 11

Page 12: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

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

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

Winter 2010/11 234319 Course 12

Page 13: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

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,1000) );end.

Version 1Version 1

Winter 2010/11 234319 Course 13

Page 14: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

Version 1Version 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,1000) );end.

What if s<0? e<0?

AuxiliaryFunction?

Winter 2010/11 234319 Course 14

Page 15: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

Version 2Version 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,1000) ); end.

What if s>e?

Winter 2010/11 234319 Course 15

Page 16: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

Version 3Version 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,1000) ); end.

Winter 2010/11 234319 Course 16

Page 17: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

Version 3Version 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,1000) ); end.

‘3’ and ‘5’ should be inputs / consts…

What is the difference? Can it be done in C/C++?

Winter 2010/11 234319 Course 17

Page 18: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

Version 4Version 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,1000, 3, 5) ); end.

Winter 2010/11 234319 Course 18

Page 19: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

Version 4Version 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,1000, 3, 5) ); end.

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

function ‘isMatching’!

Winter 2010/11 234319 Course 19

Page 20: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

Version 5Version 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,1000, 3, 5) ); end.

Winter 2010/11 234319 Course 20

Page 21: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

A more general solutionA 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.

Winter 2010/11 234319 Course 21

Page 22: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

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;...

Winter 2010/11 234319 Course

Version 6Version 6

22

Page 23: Pascal 234319 CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing

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

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

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

Winter 2010/11 234319 Course

Version 6 – cont.Version 6 – cont.

Notice the syntax – ‘@’

23