Question: Consider a subroutine swap that takes two parameters and simply swaps their values. For example, after calling swap(X,Y) , X should have the original value
Consider a subroutine swap that takes two parameters and simply swaps their values. For example, after calling swap(X,Y), X should have the original value of Y and Y the original value of X. Assume that variables to be swapped can be simple or subscripted(elements of an array), and they have the same type (integer). Show that it is impossible to write such a general-purpose swap subroutine in a language with:
(a)(12 pts) parameter passing by value.
(b)(12 pts) parameter passing by name.
Hint: for the case of passing by name, consider mutually dependent parameters.
4.(32 pts) Consider the following program, written in no particular language. Show what the program prints in the case of parameter passing by (a) value, (b) reference, (c) value-result, and (d) name. Justify your answer. When analyzing the case of passing by value-result, you may have noticed that there are two potentially ambiguous issues what a
int i;
int a[2];
p (int x, int y)
{
x++;
i++;
Y++;
}
main ()
{
a[0] = 1;
a[1] = 1;
i = 0;
p(a[i],a[i]);
print (a[0]);
print (a[1]);
}
Passed by value:1 1
Passed by reference: 2 1
Passed by value-result:
Passed by name:2 2
I'm having trouble figuring out the value-result answer. If you could also look at the other answers I've provided and verify they are correct that'd be nice.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
