Homework 3
Home Up Work My Stuff UMKC

 

 

Homework #3 - Individual
Due Date: February 14th

Points: 10

Consider the following function:

function F (x, y: integer) return integer is
begin
  x:=x+1; y:=y+1; return (x-y);
end F;
Show by one or more examples of calls on procedure F that call-by-name, call-by-value/result, and call-by-reference are different parameter-passing methods. That is, show calls that produce different results for the different binding rules.

Call-by-name

       The actual parameter is textually substituted for the corresponding formal parameter in all its occurrences in the subprogram. A call-by-name formal parameter is bound to an access method when function is called, however the actual binding to a value or and address occurs when the formal parameter is assigned.

In case of function call F (a, b) since a and  b are integers call-by-name is equivalent to call-by reference (see below).

Call-by-value/result

        The value of the actual parameter is used to initialize the corresponding formal parameter which then acts as a local variable.

The function call F (a, b) will initialize local variable x to a and local variable y to b. The function call will return (a+1)-(b+1) which is (a-b). After the function call is complete parameters a and  b will be unchanged.

Call-by-reference

        This method passes the address of the parameter to the function.

The function call F(&a,&b) will allow the function to change  parameters directly. The function call will still return (a-b), however after the call is complete, parameters a and b will be changed respectively to (a+1) and (b+1). 

Source: Sebesta,Robert W. Concepts of Programming Languages.

 

 

 
e-mail Mikhail Viron

Write to Your Representative