Class Crib Notes for CPSC 110                                Brent Dingle

                                    Monday, March 18, 2002

 

Notices:

For those that missed class on Friday before Spring Break – no new material was presented.

An attendance extra credit bonus was assigned to those who attended.

Lab assignment 5 is due Monday and Tuesday, March 25 and 26.

You will have a quiz next Wednesday, March 27.

Midterm exams should be returned today and tomorrow (March 18, 19).

There was a 2% curve on the exam.

There is an opportunity to have an additional 2% added to the midterm – see the webpage.

 

Recall that you MUST turn in a short history report as detailed in the syllabus (and on the webpage) under handouts before the last regular class period (April 30).

 

Review:

Memorize this:  ONLY variables may be sent as variable parameters.

 

For Today:

This is all in chapter 5 (refresh yourselves with local, global, scope, block)

Today we will be discussing functions. Here are the key points to remember:

FUNCTION  FunctionName( parameter list) : return_type;

VAR

    local variables declared

BEGIN

    body of the function

 

   FunctionName :=  some value;    ί This is how the function returns a value

                                                             The last value assigned to the function name is the value            

                                                             returned

END;

 

 

 

 

 

Example – Function which returns the CUBE of a number:

 

PROGRAM CubeIt;

VAR

   x, x_cubed : real;

 

FUNCTION  Cube( z : real );     { notice z is a formal VALUE parameter } 

VAR

    local_v : real;

BEGIN

   local_v := z * z * z;

   Cube := local_v;               { This line is what determines what value  the function returns}

END;

 

BEGIN  { main body }

   x := 5;

   x_cubed = Cube(x);

 

   Writeln(‘The cube of ’, x, ‘ is ’, x_cubed);

END.

 

Doing the same thing with a procedure we would have had to use TWO parameters and one of them would need to be a VARIABLE parameter. So the procedure which does the same thing as the function Cube above would be:

 

PROGRAM CubeIt2;

VAR

   x, x_cubed : real;

 

PROCEDURE  ProcCube( z : real; var v : real  );

VAR

    local_v : real;

BEGIN

   local_v := z * z * z;

   v := local_v;

END;

 

BEGIN  { main body }

   x := 5;

   ProcCube(x, x_cubed);

 

   Writeln(‘The cube of ’, x, ‘ is ’, x_cubed);

END.