When to Use Procedures If It Makes the Overall Program Easier to Understand

Pascal Midterm 2 Review

Chapter 7 - Procedures

When to use procedures – if it makes the overall program easier to understand.

Order of Declarations

Constant declarations

Variable declarations

Procedure and/or function declarations

Main part


A Simple Pascal Program with added procedure

PROGRAM SimpleWithProcedure(INPUT, OUTPUT);

CONST

MyCount = 3;

VAR a, b, c : INTEGER;

d : REAL;

PROCEDURE multiply(VAR b:INTEGER; a: INTEGER);

BEGIN

b := a * MyCount;  b=3*3 = 9

END;

BEGIN

a := MyCount;

multiply(b,a); (* calling multiply procedure*)

c := b div 3;  c = 9 div 3 = 3

d := (b + a)/ 5;  d = (3+9)/5 = 12/5=2.4

END.

A Simple Pascal Program with added procedure(formal and actual parameters)

PROGRAM SimpleWithProcedure(INPUT, OUTPUT);

CONST

MyCount = 3;

VAR a, b, c , I , j: INTEGER;

d : REAL;

(*declaring procedure with formal parameters x and y*)

PROCEDURE multiply(VAR x:INTEGER; y: INTEGER);

BEGIN

x := y * MyCount;

END;

BEGIN

a := MyCount;

j := MyCount+5;

multiply(b,a); (* calling multiply procedure with actual parameters a and b*)  b = 3*3 = 9

multiply(I,j); (* calling multiply procedure with actual parameters I and j*)  I = (3+5) * 4 = 32

c := b div 2;  c := 16 divided by 2:= 8

d := (b + a)/ 2;  d := (16+2)/ 9.0

END.

A Simple Pascal Program with added procedure(Local Variables )

PROGRAM SimpleWithProcedure(INPUT, OUTPUT);

CONST

MyCount = 3;

VAR a, b, c , I , j: INTEGER;

d : REAL;

(*declaring procedure with formal parameters x and y*)

(*procedure multiply gets the address of the first variable

and a copy of the value of the second variable*)

PROCEDURE multiply(x:INTEGER; VAR y: INTEGER);

(* z is local variable*)

VAR z: INTEGER;

BEGIN

y:=0;

FOR z := 1 TO x DO (* for z = 1 to 3 x is b *)

y := y + z * MyCount;

END;

BEGIN

a := MyCount;

b:=0;

i := MyCount+5;

j:=0;

multiply(a,b); (* calling procedure multiply with actual parameters a and b*)

multiply(I,j); (* calling procedure multiply with actual parameters I and j *)

writeln(‘ value of b= ‘ ,b);

writeln(,value of I = ‘ , I);

END.

Befor calling procedure multiply with a and b

a= 3 b is undefined

calling procedure multiply with a and b

actual parameters a and b are represented in the procedure as x and y so:

x = 3 y = 0

When for loop is executed

z = 1  y = y+ z * MyCount  y = 0 + 1 * 3 = 3

z = 2  y = y+ z * MyCount  y = 3 + 2 * 3 = 9

z = 3  y = y+ z * MyCount  y = 9 + 3 * 3 = 18

after calling procedure multiply with parameters a and be a is not changed and b = 18

In main program the statement writeln(‘ value of b= ‘ ,b); displays value of b = 18

Befor calling procedure multiply with i and j

i= MyCount + 5  3+5 = 8 j is undefined

calling procedure multiply with i and j

actual parameters i and j are represented in the procedure as x and y so:

x = 5 y = 0

When for loop is executed

z = 1  y = y+ z * MyCount  y = 0 + 1 * 3 = 3

z = 2  y = y+ z * MyCount  y = 3 + 2 * 3 = 9

z = 3  y = y+ z * MyCount  y = 9 + 3 * 3 = 18

z = 4  y = y+ z * MyCount  y= 18+ 4*3 = 30

z = 5  y = y+ z * MyCount  y = 30 + 5*3 = 45

after calling procedure multiply with parameters a and be a is not changed and b = 18

In main program the statement writeln(‘ value of j= ‘ ,j); displays value of j = 45

Chapter 8 –Nesting

Scope Rules

Begin (*Main part of the program*)

End

Procedure A can only be called in main program.

Procedure B can be called in procedure C and A but not in the main program.

http www arjay bc ca Modula 2 Text Ch10 Figure Figure 10 3 GIFProcedure C can only be called in procedure A

Functions

Functions – Functions are very similar to procedures. The Difference is that functions are employed when the result is a single value that is used directly in an expression. For example, ISVALID() is a typical function.

FUNCTION identifier parameter-list : type;

Program Exaple(input, output)

Var area: double;

Max : integer;

FUNCTION CircleArea(radius: DOUBLE): DOUBLE;

VAR area: DOUBLE;

BEGIN

area:= PI * radius * radius;

CircleArea:= area;

END;

FUNCTION Highest(v1, v2, v3: INTEGER): INTEGER;

begin

if (v1 > v2) and (v1 > v3) then

begin

result:= v1;

end

else if v2 > v3 then

begin

result:= v2;

end

else

begin

result:= v3;

end;

Highest:=result;

end;

BEGIN (*main program*)

area:= CircleArea(10.0);

maxBy2:= Highest(10, 3, 18) * 2;

END.

The function name IS NOT a variable add the assigned parameters.

Do not use a function if more than one value is returned .

Do not use var for parameters.

Do not use if inputting or outputting a value.

Use if only one value is returned

Precision

Real numbers example

Precision: numbers after . 5.4321  precision =4

sign digits.digits -34.67

Sign digits.digits E sign digits 4,321.768  4.321768×103 normalized scientfic notation 4.32176 E 3

In normalized scientific notation the exponent b is chosen so that the absolute value of a remains at least one but less than ten (1≤|a|10).

Standard decimal notation / Normalized scientific notation / Scientific E notation
2 / 2×100 / 2E 0
300 / 3×102 / 3 E 2
4,321.768 / 4.321768×103 / 4.32176 E 3
−53,000 / −5.3×104 / -5.3 E 4
6,720,000,000 / 6.72×109 / 6.72 E 9
0.2 / 2×10−1 / 2 E -1
0.00000000751 / 7.51×10−9 / 7.51 E -9

Significant digits: number of digits regardless of the whole value

Formatting real numbers:

Actual number without formatting with proper formatting variable: space used: precision

x = 4321.768 4.32176 E 3 x:8:3  4321.768

x:10:4  □□4321.7680

x:8:2  □ 4321.77

Simple Data Types

Data Types – A data type is a formal description of the values that a can be stored in a constant or variable of that type and the operations that can be applied to these values.

·  integer

·  real

·  char

·  boolean

http uploads siteduzero com files 360001 361000 360142 jpg

Ord, Pred, and Succ Functions

predefined Pascal functions that take a parameter and return a value.

Great example in naming formal and actual parameters differently.

ORD – takes an ordinal value and returns an Integer representing the value’s place in the ordering of the data type.

Examples: Integer returns integer ord(23)  23

Boolean returns 0 or 1. Ord (true)  1 ord false  0

Character returns the ascii value ord(‘A’)  66

PRED Pred – takes an ordinal value and returns the value’s predecessor (with ascii value decreased by one).

Example: PRED(‘A’)  ‘@’

PRED(‘Z’)  ‘Y’

SUCC – takes an ordinal value and returns the value’s successor( with asscii value increased by one).

Example: SUCC(‘A’)  ‘B’

SUCC (‘Z’)  ‘[‘ ascii value for ‘Z’ is 90 and for ‘[‘ is 91

CHR - Chr is the inverse of the Ord operation. It takes an integer value and returns corresponding character representation.

Example: CHR(65)  ‘A’

CHR(ord(‘A’) + 3)  CHR (65 + 3)  CHR (68)  ‘D’

Control Structures

CASE Statement -- Example:

program Grades;

var

grade: char;

begin

writeln(‘Enter Grade’);

read(grade);

CASE grade OF Look at the valiable grade

'A' : writeln('Excellent!' ); if it is = ‘A’ then writeln(‘Excellent!!’)

'B','C': writeln('Well done' ); if it is = ‘B’ or ‘C’ then writeln(‘Well done’)

'D' : writeln('You passed' ); if it is = ‘D’ then writeln(‘you passed’)

'F' : writeln('Try again' ); if it is = ‘F’ then writeln(‘Try again’)

ELSE WRITELN(‘Letter grades should be ‘A’ to ‘F’);

END; Must to use keyword ‘end’ to complete the case statement.

LOOPs

REPEAT Statement : Example

REPEAT Do it at least one time REPEAT

sum := sum + number;

statement; number := number - 1;

UNTIL number = 0;question asked

statement; if the answer is true stop

UNTIL Boolean expression ask a question if the answer is false go back up to the keyword REPEAT

For loop Example:

FOR variable:= initial_value TO final_value DO FOR i:= 1 TO 10 DO

BEGIN BEGIN

statement; writeln(i);

END; END;

Example:

FOR variable:= initial_value DOWN TO final_value DO FOR i:= 10 DOWN TO 1 DO

Begin Begin

statement; writeln(i);

end; end;

While loop

WHILE I < 10 DO

BEGIN

WRITELN(I);

I := I +1;

END


User-Defined Ordinal Data Types

program StudentMarksSystem;

CONST

NumStudents = 30;

TYPE

Range = 1.. NumStudents;

IntegerArray = array [Range] of Integer;

VAR

Class1Agrads : IntegerArray;

Class1Bgrades : IntegerArray;

Grade, ndx:INTEGER;

PROCEDURE enterGrades(StudentArray : IntegerArray)

BEGIN

FOR ndx := 1 TO NumStudents DO

BEGIN

Read (grade);

StudentArray [ndex] := grade;

END;

END; (*of enter grade procedure *)

BEGIN(*main program*)

enterGrade(MarksForClass1A);

enterGrade(MarksForClass1B);

END.(*end of main program*)

Composite Data types: Files and Arrays

Why Arrays:

Readln(inputData, value1);

Readln(inputData, value1);

Readln(inputData, value1);

.

.

Readln(inputData, value1000);

Writeln(value1000);

Writeln(value999);

Writeln(value998);

.

.

Writeln(value1);

For I :=1 to 1000 do

begin

Readln(inputData, dataAry[i];

Writeln(dataAry[i]);

End;

One-Dimensional Arrays

Defining Arrays: <arrayName> : ARRAY[n..m] OF <Data Type>;

Example: intArray : ARRAY[1..20] OF INTEGER;

charArray: ARRAY[1..20] OF CHAR;

Using for loops with arrays

Sum = 0;

FOR i:=1 TO 20 DO

BEGIN

sum:= sum + intArray[i];

END

(*different ways to see the value of an array location or use it *)

charArray[5] = ‘A’

WRITHELN(charArray[10]);

Passing arrays, always by reference

PROGRAM arrayToFunction;

CONST

size = 5;

TYPE

a = ARRAY [1..size] OF INTEGER;

VAR

balance: a;

PROCEDURE calSum( VAR arr: a);

VAR

i , sum: INTEGER;

BEGIn

sum := 0;

FOR i := 1 TO size DO

sum := sum + arr[i];

WRITELN(sum);

END;

BEGIN (* main body of program*)

WRITELN(‘calculating sum of numbers in balance array’);

(* Passing the array to the procedure *)

calSum(balance);

END.

Batch Input/Output

We need to use files to enter massive data and save the massive results.

To use files for reading and writing first we put the name of the files in the program header.

Program something(infile, outfile);

Then we declare then in var section

Var infile, outfile:TEXT

Then we open the files for reading and writing in the beginning of the program

Reset (infile); for reading

Rewrite(outfile); for writing.

To read from a file:

Read(infile, data);

To write to a file:

Write(outfile, data);