Escolar Documentos
Profissional Documentos
Cultura Documentos
end;
A function that doesnt return anything is a procedure.
procedure myProc();
begin
end;
Winter 2010/11 234319 Course 11
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
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 Version 11
Winter 2010/11 234319 Course 13
Version Version 11
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.
What if s<0? e<0?
Auxiliary
Function?
Winter 2010/11 234319 Course 14
Version Version 22
program Sum;
type type type type positiveInt positiveInt positiveInt positiveInt = = = = 1 11 1..MAXINT; ..MAXINT; ..MAXINT; ..MAXINT;
function isMatching isMatching isMatching isMatching(i : integer) : boolean;
begin
isMatching := ((i mod 3 = 0) or (i mod 5 = 0));
end;
function sumOfMatching(s, e : positiveInt positiveInt positiveInt positiveInt) : integer;
var sum, i : integer;
begin
sum := 0; for i := s to e do begin
if ( isMatching isMatching isMatching isMatching( (( (i ii 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
Version Version 33
program Sum;
type positiveInt = 1..MAXINT;
function SumOfMatching(s, e : positiveInt) : Integer;
var sum, i : integer;
function function function function isMatching isMatching isMatching isMatching( (( (i ii i : integer) : : integer) : : integer) : : integer) : boolean boolean boolean boolean; ; ; ;
begin begin begin begin
isMatching isMatching isMatching isMatching := (( := (( := (( := ((i ii i mod mod mod mod 3 33 3 = = = = 0 00 0) or ( ) or ( ) or ( ) or (i ii i mod mod mod mod 5 55 5 = = = = 0 00 0)); )); )); ));
end; end; end; 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
Version Version 33
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,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
Version Version 44
program Sum;
type positiveInt = 1..MAXINT;
function sumOfMatching(s,e,div div div div1 11 1,div ,div ,div ,div2 22 2:positiveInt):integer;
var sum, i : integer;
function isMatching(i , d dd d1 11 1, d , d , d , d2 2 2 2 : integer) : boolean;
begin
isMatching := ((i mod d dd d1 11 1=0) or (i mod d dd d2 22 2=0));
end;
begin
sum := 0; for i := s to e do begin
if (isMatching(i,div div div div1 11 1,div ,div ,div ,div2 22 2)) then sum:=sum+i;
end;
sumOfMatching := sum;
end;
begin WriteLn( sumOfMatching(1,1000, , , , 3 33 3, , , , 5 55 5) ); end.
Winter 2010/11 234319 Course 18
Version Version 44
program Sum;
type positiveInt = 1..MAXINT;
function sumOfMatching(s,e,div div div div1 11 1,div ,div ,div ,div2 22 2:positiveInt):integer;
var sum, i : integer;
function isMatching(i , d dd d1 11 1, d , d , d , d2 2 2 2 : integer) : boolean;
begin
isMatching := ((i mod d dd d1 11 1=0) or (i mod d dd d2 22 2=0));
end;
begin
sum := 0; for i := s to e do begin
if (isMatching(i,div div div div1 11 1,div ,div ,div ,div2 22 2)) then sum:=sum+i;
end;
sumOfMatching := sum;
end;
begin WriteLn( sumOfMatching(1,1000, , , , 3 33 3, , , , 5 55 5) ); end.
div1 and div2 are
already known to nested
function isMatching!
Winter 2010/11 234319 Course 19
Version Version 55
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 div div div div1 11 1=0) or (i mod div div div div2 22 2=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
A more general solution 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 integerboolean function we desire.
Winter 2010/11 234319 Course 21
program Sum;
type positiveInt = 1..MAXINT;
type matcher = function ( i:integer ) : matcher = function ( i:integer ) : matcher = function ( i:integer ) : matcher = function ( i:integer ) : boolean boolean boolean 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 Version 66
22
...
function sumOfMatching( s, e : positiveInt ;
isMatching isMatching isMatching isMatching : matcher : matcher : matcher : matcher ) : integer;
var sum, i : Integer;
begin
...
for i := s to e do begin
if ( isMatching isMatching isMatching isMatching( (( (i ii i) ) ) ) )
then sum := sum + i;
end;
...
end;
begin
WriteLn( sumOfMatching(1, 1000, @m @m @m @m1 11 1) );
end.
Winter 2010/11 234319 Course
Version Version 6 6 cont. cont.
Notice the syntax @
23
So why learn So why learn Pascal Pascal?? ??
Still quite useful
Safety
Mixing types leads to errors.
No type casting and no pointer arithmetic.
Speed and Size
Pascal compiler still fast.
Teaching Purposes
Shown to be easier to learn.
Less overhead and fewer ways for a student to get a
program into trouble.
Winter 2010/11 234319 Course 24
So why learn So why learn Pascal Pascal?? ?? cont. cont.
Still in the market
Many small-scale freeware, shareware, and open-
source programs are written in Pascal or in
Delphi (Object Pascal).
We will use Pascal and Pascal-like languages in
many examples and most likely in some of the
exam questions.
So you need to know the basics of it
Winter 2010/11 234319 Course 25