1
$\begingroup$

Given the question below:

Consider that, on the procedure below, the variable X is defined globally and the parameters of the vr function are passed by value

procedure vr(u,v) {
   u = 2*u;
   x = u+v;
   u = u - 1; 
}

Define the value of x in the end of the following:

x = 4;
y = 2;
vr(x,y);
print(x);

I always knew that parameters passed by value were actually a copy of the original value, but in this case i can't tell what scope is being used for the function.

I also would like to read more about cases like that. If you can, please leave any suggestive material so i can get more in depth of this subject.

I know that this is a very bad practice of naming variables and should be avoided, but in this case i guess it was intentional for the problem.

$\endgroup$
1
$\begingroup$

You can unroll the procedure:

x = 4;
y = 2;
u = x;
v = y;
u = 2*u;
x = u+v;
u = u-1;
print x;

This is semantically equivalent to your program.

Once you copy the value of x to u, the link between the two disappears. This is in contrast to references in C++ or mutable objects in python, which are really pointers; if u were a reference (i.e., if parameters were passed by reference), then the assignment u = x would have identified the two variables as pointing at the same location in memory. As it is, u is just a value (since parameters are passed by value), and so this linkage is absent.

$\endgroup$

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.