Class Crib Notes for CPSC 110                                Brent Dingle

                                    Monday, February  25, 2002

Reminders:

Quiz Wed., Feb. 27

 

Review:

Still going through procedures

 

New Stuff for today

More procedures

 

Some Key Words to watch for:

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

 

Last time our procedures were using global variables. This is bad. Very bad.
 
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.

 

Next time

More procedures.