Lecture 28 - December 4, 2007

 

Returned some work

 

Went over the November 29th quiz

Note:  given the particular precedence rules in the table the correct answer for      -a  +   (b * c)   should have been

 

         ( - ( a +  ( b * c) 1) 2 ) 3

 

Some of you got credit for    ( ( -a)1  + (b * c)2  )3  which is not correct due the the unary minus being of lower precedence than * and +  which is unusual.

 

Went over the parameter passing modes using the .jpg files from lecture 27.

 

 

Note the following:

  • Although Ada does not let you modify IN parameters (parameters passed by value) other languages may.  In fact,  Pascal does let you modify its value parameters.  Thus, the Pass by Value  example will let you modify the parameters X and Y internally in the subprogram but the modified values will not be passed back.
  • Although Ada does let you access its OUT parameters (parameters passed by result), by accessing whatever is stored in the corresponding memory location for the parameter name.  In general, languages which implement pass by result will give you an error message if you try to access a parameter classified as a result parameter.  Thus,  the Pass by Result example will give you an error message when you attempt to store X in TEMP in the SWAP_BY_RESULT procedure.
  • Pass by Name does a textual substitution of the actual parameter names for the formal parameter names upon entry to the subprogram.  Textual substitutions can be performed in C and C++ using a DEFINE statement but those are not parameter examples.
  • Pass by Location  is another name for Pass by Reference and the formal parameters have pointers to the actual parameter locations and thus any changes to the formal parameters occur immediately to the corresponding actual parameters.
  • Pass by Value-Result  parameters copy the values of the actual parameters into the subprograms at the start of their execution and copy the values in the formal parameters back to the actual parameter locations at the end of the subprogram execution.

 

 

Below is  an Ada program which shows that OUT parameters are RESULT parameters but for convenience can also be accessed within the subprogram.  The Ada program also shows that IN parameters are VALUE parameters whose values may not be changed within the subprogram.   Ada's IN OUT parameters behave similarly to LOCATION (i.e. REFERENCE) parameters as long as the subprogram terminates normally (i.e. without an exception occurring).  If an exception occurs that causes the subprogram to terminate abnormally, the altered parameter values will not be copied back to the calling program.


 

with ada.text_io;
with ada.integer_text_io;
procedure testOUTmode is
   x, y, z : integer;
  
procedure swop (x :in integer; -- x's value will be passed in;

                                  --can't be modified
            y : in out integer;   -- y parameter is a value result parameter
 
          z : out integer) is  -- z is a pseudo pass by result

                                  -- copy value out parameter
        
  
begin
 
   ada.text_io.put_line (" in subprogram values of x, y, and z are ");
     ada.integer_text_io.put (x);
     ada.integer_text_io.put (y);
     ada.integer_text_io.put (z);
     ada.text_io.new_line;
     ada.text_io.put_line (
" in subprogram changed values of x,y, and z are ");
    
--x := 18;
    
y := 27;
     z := 13; 
     ada.integer_text_io.put (x);

     ada.integer_text_io.put (y);
     ada.integer_text_io.put (z);
     ada.text_io.new_line;
      
end swop;
      
      
begin
          x := 5;
          y := 10;
          z := 15;
          ada.text_io.put_line

            (" in main program before call, values of x, y, and z are ");
          ada.integer_text_io.put (x);
          ada.integer_text_io.put (y);
          ada.integer_text_io.put (z);
          ada.text_io.new_line;
          swop (x, y, z);
          ada.text_io.put_line

            (" in main program after call, values of x, y, and z are ");
          ada.integer_text_io.put (x);
          ada.integer_text_io.put (y);
          ada.integer_text_io.put (z);
      
end testOUTmode;

 

Card stock summary sheets were collected today.

Some of you forgot to indicate the name of your language on the summary sheet (strange??)

In class we listed (discussed) a few other things you might have left off

            the parameter passing modes

            the scope rules (i.e. is non-local access allowed ??)

            subprogram nesting rules

built-in functions examples - what categories are there (i.e.  arithmetic, string processing, …)

             the paradigm of your language

(i.e. procedural, functional, logic programming, string processing, object-oriented)

             Data types (i.e. objects)  -  composite, simple

             Exception handling of any kind?