CS441

Assignments

HomeWork 3.

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.

Answer

1. call-by-name

This is a method where arguement expressions are passed unevaluated

F((a+4),(a+3))

begin

x=a+4+1:(x=a+5)

y=a+3+1:(x=a+4)

return((a+5)-(a+4)) (return=1)

2. call-by-value/result

This method involves transfer of arguements to a function using call by value, with results copied back to the caller program in the same manner.

Answer

F(3,2)

x=3+1:(x=4)

x=2+1:(x=3)

return(4-3) (return=1)

x=4,y=3 are also returned to the calling program thus altering the variables

3. call-by-reference

Only addresses of variables are involved. So a copy of the variable values is not necessary

answer

F(3,2)

x=3+1: (x=4)

y=2+1 (y=3)

return(4-3) (return =1)

Last Modified: February 13, 2002
Alex Omorodion,