Site hosted by Angelfire.com: Build your free website today!

 

 

KAT OREJAS WEB BLOG

 

 

 

Chapter 8

Summary:

 

Parameter-Passing Methods of the Major Languages

 

FORTRAN has always used the inout-mode semantics model of parameter passing, but the languages does not specify whether pass-by-reference or pass-by-value-result should be used.

          ALGOL 60 introduced the pass-by-name method. It also allows pass-by-value as an option. Primarily because of the difficulty in implementing them, pass-by-name parameters were not carried from ALGOL 60 to any subsequent languages that became popular, other than SIMULA-67.

          C uses pass-by-value. Pass-by-reference semantics is achieved by using pointers as parameters. C copied this from ALGOL 68. In both C and C++, formal parameters can be typed as pointers to constants. The corresponding actual parameters need not to be constant, for in such cases they are coerced to constants. This allows the pointer parameters to provide the efficiency of pass-by-value.

          C++ includes a special pointer type, called a reference type. Reference parameters re implicitly dereferenced, and their semantics are pass-by-reference. It also allows reference parameters to be defined to be constants

 

EXAMPLE:

                   Void fun(const int &pl, int p2, int &p3) { …}

 

Where p1 is pass-by-reference but cannot be changed in the function fun, parameter p2 is pass-by-value, and p3 is pass-by-reference. Neither p1 nor p3 need be explicitly dereferenced in fun.

 

          Constant parameters and in–mode parameters are not exactly alike. Constant parameters clearly implement in mode. However, in all of the common imperative languages except Ada, in-mode parameters can be assigned in the subprogram even though those changes are never reflected in the values of the corresponding actual parameters. Constant parameters can never be assigned.

          As with C and C++, all Java parameters are passed by value. However because objects can only be accessed through reference variables, object parameters are in effect passed by referenced. Likewise, because reference variables cannot point to scalars variables directly and java does not have pointers, scalars cannot be passed by reference in Java(through an object that contains a scalar can).

          ALGOL W(Wirth and Hoare, 1966) introduced the pass-by-value result method of parameters passing as an alternative to the inefficiency of pass-by-name and the problems of pass-by-reference.

          In PASCAL and MODULA-2, the default parameter-passing method is passing-by-value, and pass-by-reference can be specified by prefacing formal parameters with the reserved word var.

          The designer of Ada defined versions of the semantics modes of parameter transmission: in, out, and inout. The three modes are appropriately named with the reserved words in, out, and in out, where in is the default method.

 

 For example, consider the following Ada header:

 

                   Procedure ADDER(A : in out INTEGER);

                                                B : in INTEGER;

                                                C: out FLOAT)

 

 

Ada format parameters declared to be out mode can be assigned but not referenced. Parameter that are in mode can be referenced but not assigned. Quite naturally, in out mode parameters can be both referenced and assigned.

 

 

Type-Checking Parameters

 

It is now widely accepted that software reliability demands that the types of actual parameters be checked for consistency with the types of the corresponding  formal parameters. Without  such types checking , small typographical errors can lead to program errors that may be difficult to diagnose because they are mot detected by the compiler or the run-time system. For example the function call

                  

          RESULT := SUB (1)

 

The actual parameter is an integer constant. If the formal parameter of the SUB1 is a floating point type checking. Although an integer 1 and a floating point 1 have the same value, the representations of these two are very different. SUB1 cannot produce a correct result given an integer actual parameter value when it expects a floating point value.

 

          FORTRAN 77 does not require parameters type checking; Pascal, modula-2, FORTRAN 90, Java, and Ada do require it.

          C and C++ require some special discussion in the matter of parameter type checking. In the original C, neither the number of parameters nor their types were checked. In ANSI C, the formal parameters of functions can be defined in two ways. First, they can be  as in the original C; that is the names of the parameters are listed in parenthesis and the type declarations for them follow, as in

 

          double sin(x)

double x;

{ …}

 

Using this method avoid type checking, threby allowing calls such as

 

          Double valuie;

          Int count;

          …

          value = sin(count);

to be legal but nonsense.

          The alternative is called prototype method, in which the formal parameter types are included in the list, as in

 

          Double sin(double x)

          { …}

If this version of sin is called with the same cell as above, that is,

          Value = sin(count);

It is also legal. The type of the actual parameter(int) is checked against that of the formal parameter(double). Although they do not march,int is coercible or if the number of parameters is wrong, then a syntax error is detected. So in Ansi C, the user chooses whether he or she wants parameters to be type checked.

          In C++, all functions must have their formal parameters in prototype form. However, type checking can be avoided for some of the parameters by replacing the last part of the parameter list with an ellipsis, as in

 

          printf(const char* …);

 

A call to printf must include at least one parameter, a pointer to a constant character string. The way printf determines whether there are additional parameter is by the presence of special symbols in the string parameter.

 

Implementing Parameter-Passing Methods

 

        In ALGOL 60 and its descendant language, parameters communication takes place through the run time stack. The run time stack is initialized and maintained by the run time system, which is the system program that manages the execution of programs. The run time stack is used extensively for sub program control linkage and passing.

          Pass-by-value parameters have their values copied into stack locations. The stack location then serve as storage for the corresponding forma parameters.

Pass-by-result actual parameters are implemented as the opposite of pass-by-value.

Pass-by-value result parameters can be implemented directly from their semantics as a combination of pass-by-value and pass-by-result. The stack location for the parameters is initialized by the call and is then used like a local variable in the called subprogram.

Pass by reference parameters are perhaps the simplest to implement. Regardless of the type of the actual parameters, only its address must be placed in the stack. a subtle but fatal error can occur with the pass-by-value-result parameters if care is not taken in their implementation.

Pass-by-name parameters are usually implemented with the parameterless procedures or code segments called thunks. A thunk must ve called for every reference to a pass- by- name parameter in the called subprogram. The thunks evaluate the reference in the proper referencing environment. Which is that of the subprogram that passed the actual parameter. Thunks are bound to their referencing environment at the time of the call that passed by name parameter. A thunk returns the address of the actual parameter reference is in an expression, the code of the reference must include the necessary dereference to get the value from the cell whose address was returned by the thunk . althogether, this is the a costly process, relative to simple indirect addressing used by reference parameters.

 

 

Multidimensional arrays as Parameters

 

The storage mapping functions that are used to map the index values of reference to elements of multidimensional arrays to address I memory. In some languages such as C and C++, when a multidimensional arrays are passed as a parameter to a subprogram, the compiler must be able to build the mapping functions for that array while seeing only the  text of the subprogram. Consider the problem of passing a matrix to a function in C. Multidimensional arrays in Care really arrays of arrays and they are stored now in major order. The mapping function for row major order for two dimensions needs the number of columns but not the number of rows.

 

Illustrations:

 

          void fun(int natrix{}[10]){

                   …}

          void main (){

                   int mat[5] [10];

                   …

          fun (mat);

                   …

}

 

The problem with this parameters is that it does not allow the programmer to write a function that can accept matrixes with different numbers of columns; a new function must be written for every matrix with the a different number of columns. This, in effect, disallows the writing flexible functions that may effectively reusable if the function deal with multidimensional arrays.

 

Example:

          Void fun(float *mat_ptr, int num_rows, int num_cols);

 

The following statement can be used  to move the value of the variable x to the [row] [col] element of the parameter matrix in fun:

         

*(mat_ptr + (row * num_cols) + col = x;

Although the works, it is obviously difficult to read , and because of its complexity, it is error prone. The difficulty in reading this can be alleviated by using a macro to define the storage-mapping function, such as,

 

#define mat_ptr(r,c)

                   (*(mat_ptr + ((r) * num_cols) + (c)))

 

With this, the assignment above can be written as

 

Mat_ptr(row, col) =x;

 

 

Design Considerations

 

Two important considerations are involved in choosing the parameter passing method: efficiency and whether one-way or two-way data transfer is needed.

          Contemporary software engineering principles dictate that access by subprogram code to data outside the subprogram be minimized. With this goal in mind, in-mode parameters to the caller. Out-mode parameters should be used when no data are transferred to the called subprogram but the subprogram must transmit data back to the caller. Finally, inout-mode parameters should be used only when data must move in both directions between the caller and the called subprogram.

          There is a practical consideration that is conflict with this principle. Sometimes it is justified to pass access path for one way parameter transmission. For example when a large arrays is to passed to a subprogram that does not modify it , a one way method may be preferred. However the pass-by-value would require that the entire array be move to a local storage area of the subprogram. This would be costly in both the time and space. Because of this, large arrays are often passed by reference. This is precisely why Ada 83 definition allows implementors to choose between the two methods for structured parameters. C++ constant reference parameter offer another solution. Another alternative approach would be to allow the user to choose between the methods.

The choice of parameter-passing method for function is related to another design issue: functional side effects.