Pascal Course Spring 20141
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 Spring 2014Course These concepts will be explained in the lectures 2
program HelloWorld; uses crt; const hello = 'Hello, World'; procedure sayHello(c:char); begin WriteLn(hello, c); end; begin sayHello('!'); readkey; end. A basic Pascal program Spring 2014Course
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 2014Course
Spring 2014Course Flow Control If x < 2 then write(x); The expression between the “if” and “then” must be of type Boolean. If x < 2 then begin write(x); end If x < 2 then write(x) // no semicolon! Else write(y);
Spring 2014Course Flow Control case i of 1 : write(“A”); 2 : write(“B”); 3 : write(“C”) // no semicolon end Case works well with more complicated data types, as we will see.
Spring 2014Course 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 do begin WriteLn(i); end;
Data Types We can create our own types: – Enumerated types: type Color = (Red, Green, Blue, Yellow); Enumerated types are ordered: Red < Blue = true ord(Yellow) = 3 – Subrange types: type Letter = ‘A’.. ’Z’; Index = 3.. 8; BasicColor = Red.. Blue; – Subset types: type Rainbow = set of Color; Spring 2014Course succ(Red) = Green pred(Blue) = Green
Data Types - cont. – Records (similar to C structs - one of Pascal’s original inventions) type date = record day : ; month : ( January, February, March, April, May, June, July, August, September, October, November, December); year : ; end; – Variant records Spring 2014Course
Arrays in Pascal Spring 2014Course Pascal arrays are defined as follow: array [ ] of 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 value end; – 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 itself end; Spring 2014Course
A simple problem… Given a range of positive numbers: – Summarize all numbers in range that divide by 3 or 5. – Print the result. Spring 2014Course
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 2014Course
Version 1 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. What if s<0? e<0? Auxiliary Function? Spring 2014Course
Version 2 program 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 2014Course
Version 3 program 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 2014Course Can it be done in C/C++?
Version 4 program 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 2014Course
Version 5 program 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 2014Course
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 2014Course
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 2014Course Version 6 20
... 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, ); end. Spring 2014Course Version 6 – cont. Notice the syntax – Generally “address of” 21
Multiline: – { … } – {* … *} – (* … *) Single line – // … Spring 2014Course Some comments 22
Exceptions Generics Tuples Foreach loops Named parameters Classes… Coroutines / generators Closure Garbage collection Varags, variable-length arrays Spring 2014Course What’s missing? 23