Reputation: 403
I am given the following code and asked what it will print out depending on how it is called:
int a;
procedure foo(int x){
x = x + 10;
a = a + x;
}
procedure fie( ){
a = 5;
foo(a);
print (a);
It asks for call by value
and call by value-result
For call by value I know that it would get 20 because you end up adding 10 to x which carries the value 5, then adding 15 to the original a which becomes 20.
My problem is why does the call by value-result
print 15? Can someone give me a good explanation, I am confused on how exactly value-result
and value
differ from each other and how value-result
is actually interpreted.
Upvotes: 1
Views: 12500
Reputation: 422
NOTE: My previous answer was wrong. It has now been corrected.
C/C++ do not implement anything like Call-By-Value-Result (natively). As I understand it, that's an ADA term (Working on government code?). Call-By-Value would result in 15, Call-By-Reference is the trickiest one, because you would be modifying a
in line 3 as well as line 4 of your example.
--EDIT--
I have implemented the following code, which I believe to be a valid representation of the syntax described (I'm not familiar enough with ADA to do the test directly, maybe someone else can). As you will see, the result for By-Value-Result is 15.
#include <iostream>
int a_v, a_r, a_vr;
void foo_by_val(int x)
{
x = x + 10;
a_v = a_v + x;
}
void foo_by_ref(int &x)
{
x = x + 10;
a_r = a_r + x;
}
void foo_by_value_result(int &x_ref)
{
int x = x_ref;
x = x + 10;
a_vr = a_vr + x;
// x = x_ref; <-- My previous mistake
x_ref = x;
}
void fie( ){
a_v = 5;
a_r = 5;
a_vr = 5;
foo_by_val(a_v);
foo_by_ref(a_r);
foo_by_value_result(a_vr);
std::cout << "By Value: " << a_v << std::endl;
std::cout << "By Refference: " << a_r << std::endl;
std::cout << "By Value Result: " << a_vr << std::endl;
}
int main()
{
fie();
return 0;
}
Result:
By Value: 20
By Reference: 30
By Value Result: 15
Upvotes: 2
Reputation: 1396
The given example is only about 'call by value' and that is why the printed value is 20. In case of 'call by value-result' actual parameter supplied by the caller is copied into the callee's formal parameter; Then when the function is run the formal parameter could be modified anywhere inside the function body but only when function exits the modified formal parameter is then copied back to the caller. In case of 'call by reference' an 'alias' is created between the formal and actual parameters and any modification to formal parameter is reflected to the caller parameter.
Having the code in a language that support 'call by value-result'
void test(IN OUT int c; REF int d)
{
printf ("a: %d b: %d c: %d d: %d\n", a,b,c,d);
a = 0; b = 9; c = 4; d = 6;
printf ("a: %d b: %d c: %d d: %d\n", a,b,c,d);
}
void main(void)
{
int a = 5, b = 7;
test(a,b);
printf ("a: %d b: %d\n",a,b);
}
The results of this should be
a: 5 b: 7 c: 5 d: 7
a: 0 b: 6 c: 4 d: 6 // d and b are aliases, so b=9 is modified by d = 6
a: 4 b: 6 // a replaced with value of c on function exit.
So in your case : call by value
a = 5 when you enter the function
x = 5 + 10 = 15
then we have 'a' as global variable
a = 5 + x = 5 + 15 = 20
then on print a = 20
in case of call by value- result when entering the function
x = 5 + 10 = 15
a = 5 + 15 = 20
but when the function is return the value if x is copied on 'a' which result a = 15 then the print is 15
Upvotes: 6