CHAPTER 8

SUBPROGRAMS

 

INTRO:

 

FUNDAMENTALS OF SUBPROGRAMS

Subprograms include the following characteristics:

 

BASIC DEFINITIONS

 

PARAMETERS

 

PROCEDURES AND FUNCTIONS

 

DESIGN ISSUES FOR SUBPROGRAMS

 

LOCAL REFERENCING ENVIRONMENTS

 

PARMETER-PASSING METHODS

 

PASS BY VALUE

                        

                        Caller                                             Callee

                                                     

                 (sub(a, b, c))                                   (procedure sub(x, y, z))  

 

                                                  

                           a   ----------call-----------------à x

                    IN-MODE

                                                

                         

                          b  ß-------return----------------- y

                  OUT-MODE

 

                           

                            c  -----------call--------------à z

                              ß--------return-----------------

                  INOUT-MODE

 

 

PASS BY RESULT

              sub(p1, p2)

 

PASS BY VALUE RESULT

 

PASS BY REFERENCE

 

PASS BY NAME

 

MULTIDIMENSIONAL ARRAYS AS PARAMETERS

·         In some languages like C or C++, when a multidimensional array is passed as a parameter to a subprogram, the compiler must be able to build the mapping function for that array while seeing only the text of the subprogram.  This is true because the subprograms can be compiled separately from the programs that call them.

·         The problem with this method of passing matrices as parameters is that it does not allow the programmer t write a function that can accept matrices with different numbers of columns – a new function must be written for every matrix with a different number of columns.  This disallows writing flexible functions that may be effectively reusable if the functions deal with multidimensional arrays.

 

OVERLOADED SUBPROGRAMS

·         An overloaded subprogram is a subprogram that has the same name as another subprogram in the same referencing environment

·         Every version of an overloaded subprogram must have a unique protocol, that is, it must be different from the others in the number, order, or types of its parameters, or in its return types if it is a function

·         C++, Java, and Ada include predefined overloaded subprograms

 

GENERIC SUBPROGRAMS

·         A generic or polymorphic subprogram takes parameters of different types on different activations.

·         Overloaded subprograms provide a particular kind of polymorphism called ad hoc polymorphism.

·         Parametric polymorphism is provided by a subprogram that takes a generic parameter that is used in a type expression that describes the types of the parameter of the subprogram.

·         Ada and C++ provide a kind of compile-time parametric polymorphism.

 

ACCESSING NONLOCAL ENVIRONMENTS

·         The non-local variables of a subprogram are those that are visible within the subprogram but are not locally declared

·         Global variables are those that are visible in all program units.