|
Homework #3 - Individual Consider the following function: function F (x, y: integer) return integer isShow 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.
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).
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.
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 |