Class Crib Notes for CPSC 110                               Brent Dingle

                                    Wednesday, February  20, 2002

Reminders:

Quiz Next Week ŕ Wed., Feb. 27

No class on Friday,  Feb. 22 due to relocation =).

Review:

Last time we went through Top Down Design

 

New Stuff for today

Procedures (chap 4)

 

Some Key Words to watch for:

Global Variables, Local Variables, Scope, Formal Parameters, Actual Parameters, Variable Parameters, Value Parameters, Parameter List

 

Example 1:

Task:

Write a program that will infinitely ask for people’s names and say hello to them.

 

PROGRAM ProcEx;

VAR

   first_name, last_name : string;

 

BEGIN   { main body }

  

   While (true) DO                    { notice this is an infinite loop } 

   Begin

      { Get Name }

      write(‘Please enter your first name -> ‘);

      readln(first_name);

      write(‘Please enter your last name -> ‘);

      readln(last_name);

 

      { Say hello } 

      writeln(first_name, last_name, ‘… such a nice name.’);

      writeln(‘You must be a great person.’);

      writeln;

   End;

END.

 

So we could break our main task into 2 subtasks:

GetName and SayHello

 

Notice both subtasks SHOULD require parameters of first_name and  and last_name. We will get back to that.

 
So let’s create a new program using PROCEDURES to implement our subtasks
Our first version will NOT use parameters but make use of the global scope of GLOBAL variables
 

PROGRAM ProcEx2;

VAR

   first_name, last_name : string;

 

PROCEDURE GetName

BEGIN

   write(‘Please enter your first name -> ‘);

   readln(first_name);

   write(‘Please enter your last name -> ‘);

   readln(last_name);

END;

 

 

PROCEDURE SayHello;

BEGIN

   writeln(first_name, last_name, ‘… such a nice name.’);

   writeln(‘You must be a great person.’);

END;   { end of proc Message }

 

 

 

BEGIN   { main body }

  

   WHILE (true) DO

   Begin

      GetName;

      SayHello;

   End;

END.

 

Now using  global variables in procedures is BAD, VERY BAD.

Altering global variables in procedures is even WORSE.

 
So we want to send parameters to the procedures instead. Notice a couple things:
 

If a procedure can alter a given parameter that parameter is called a VARIABLE PARAMETER.

If a procedure cannot alter a given parameter that parameter is called a VALUE PARAMETER.

So now let’s write that program:

 
 

PROGRAM ProcEx3;

VAR

   first_name, last_name : string;

 

{ notice the VAR in front of the variable name means VARIABLE parameter }

{ notice also the variable names duplicate the global var names – they don’t have to }

PROCEDURE GetName(VAR first_name : string; VAR last_name : string);

BEGIN

   write(‘Please enter your first name -> ‘);

   readln(first_name);

   write(‘Please enter your last name -> ‘);

   readln(last_name);

END;

 

{ Note: there is no VAR before either variable name, note also the names are NOT the

 same as the global names }

 

PROCEDURE SayHello(fname : string; lname : string);

BEGIN

   writeln(fname, lname, ‘… such a nice name.’);

   writeln(‘You must be a great person.’);

END;   { end of proc Message }

 

 

 

BEGIN   { main body }

  

   WHILE (true) DO

   Begin

      GetName(first_name, last_name);    { note we MUST send first_name first and last_name second }

      SayHello(first_name, last_name);     { else we will get them mixed around Bob Smith becomes Smith Bob }

   End;

END.

 
And we may get into an more complicated example.
 

Next time

Procedures, maybe identifiers some more definitions about scope, continuing examples