OOPS - Parameterization Mechanisms, Study notes of Object Oriented Programming

Detailed informtion about Type parameters and parameterized types, Parameters, actual parameters, Parameterization mechanisms, Parameter-Passing Methods, Semantic Models of Parameter Passing.

Typology: Study notes

2010/2011

Uploaded on 09/05/2011

vrunda
vrunda 🇮🇳

4.1

(21)

76 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Type parameters and parameterized types
Parameters
The parameters in the subprogram header are called formal parameters. They
are sometimes thought of as dummy variables because they are not variables in the usual
sense: In some cases, they are bound to storage only when the subprogram is called, and
that binding is often through some other program variables.
Subprogram call statements must include the name of the subprogram and a list of
parameters to be bound to the formal parameters of the subprogram. These parameters
are called actual parameters.
The binding of actual parameters to formal parameters is done by simple position:
The first actual parameters is bound to the first formal parameter and so forth. Such
parameters are called positional parameter.
When lists are long, however it is easy for the program writer to make mistakes in
the order of parameters in the list. One solution to this problem is to provide keyword
parameters, in which the name of formal parameter to which an actual parameter is to be
bound is specified with actual parameter. The advantage of keyword parameters is that
they can appear in any order in the actual parameter list.
SUMER(LENGTH => MY_LENGTH,
LIST => MY_ARRAY,
SUM => MY_SUM);
PAGE 4
pf3
pf4
pf5

Partial preview of the text

Download OOPS - Parameterization Mechanisms and more Study notes Object Oriented Programming in PDF only on Docsity!

Type parameters and parameterized types Parameters The parameters in the subprogram header are called formal parameters. They are sometimes thought of as dummy variables because they are not variables in the usual sense: In some cases, they are bound to storage only when the subprogram is called, and that binding is often through some other program variables. Subprogram call statements must include the name of the subprogram and a list of parameters to be bound to the formal parameters of the subprogram. These parameters are called actual parameters. The binding of actual parameters to formal parameters is done by simple position: The first actual parameters is bound to the first formal parameter and so forth. Such parameters are called positional parameter. When lists are long, however it is easy for the program writer to make mistakes in the order of parameters in the list. One solution to this problem is to provide keyword parameters , in which the name of formal parameter to which an actual parameter is to be bound is specified with actual parameter. The advantage of keyword parameters is that they can appear in any order in the actual parameter list. SUMER(LENGTH => MY_LENGTH, LIST => MY_ARRAY, SUM => MY_SUM);

Parameterization mechanisms Parameter-Passing Methods Parameter-passing methods are the ways in which parameters are transmitted to and/or from called subprograms.

Semantic Models of Parameter Passing Formal parameters are characterized by one of three distinct semantics models : (1) They can receive data from the corresponding actual parameter, in mode (2) They can transmit data to the actual parameter, out mode, (3) They can do both. inout mode

Implementation Models of Parameter Passing

Pass-By-Value ( In-Mode)

  • When a parameter is passed by value, the value of the actual parameter is used to initialize the corresponding formal parameter, which then acts as a local variable in the subprogram, thus implementing in-mode semantics.
  • Pass-by-value is normally implemented by actual data transfer, because accesses are usually more efficient with this method.
  • The main disadvantage of the pass-by-value method if physical moves are done is that additional storage is required for the formal parameter. E.g in C language
    1. Function Calling square (5); or int n = 5; square (n);
    2. Definition int square( int x) { return x * x; }

subprogram termination, the value of the formal parameter is transmitted back to the actual parameter.

  • Pass-by-value-result shares with pass-by-value and pass-by-result the disadvantages of requiring multiple storage for parameters and time for copying values. E.g
  1. Function Calling int a,b,c = 5; add (a,b,c); cout<< c;
    1. Definition int add( int x, int y, int z) { z = x + y; }

Pass-By-Reference

  • (^) Pass-by-reference is a second implementation model for inout-mode parameters. Rather than transmitting data values back and forth, however, as in pass-by-value- result, the pass-by-reference method transmits an access path, usually just an address, to the called subprogram. This provides the access path to the cell storing the actual parameter. Thus the called subprogram is allowed to access the actual parameter in the calling program unit. In effect, the actual parameter is shared with the called subprogram.
  • The advantage of pass-by-reference is that the passing process itself is efficient, in terms of both time and space. Duplicate space is not required, nor is any copying. Collisions can occur between actual parameters.
  • Consider a C function procedure that has two parameters that are to be passed by reference, as in **void fun(int first, int second) If the call to fun happens to pass the same variable twice, as in fun(&total, &total). Then first and second in fun will be aliases.

Example program in C Language #include <stdio.h> void swapnum(int *i, int *j) { int temp = i; ij == j;temp; } int main(void) { int a = 10; int b = 20; swapnum(&a, &b); printf("A is %d and B is %d\n", a, b); return 0; }

Pass-By-Name

  • Pass-by-name is an inout-mode parameter transmission method that does not correspond to a single implementation model, as explained below.
  • When parameters are passed by name, the actual parameter is in effect, textually substituted for the corresponding formal parameter in all its occurrences in the subprogram. This is quite different from the methods discussed thus far.
  • In those cases, formal parameters are bound to actual values or addresses at the time of the subprogram call.
  • A pass-by-name formal parameter is bound to an access method at the time of the subprogram call, but the actual binding to a value or an address is delayed until the formal parameter is assigned or referenced.

E.g Algol uses the powerful mechanism pass-by-name.

  1. A sample program: procedure swap (a, b); integer a, b, temp; begin temp := a; a := b; b:= temp end;
  2. Effect of the call swap(x, y):