In: Computer Science
Assume that the following Ada like program compile successfully.
int k = 0; void f(int p) { int main () { |
What is printed on the screen assuming?
Solution:
1. 11112
2. 13992
Caller is the function which calls another function callee
In pass by value-result, just before the control is transferred back to the caller by the callee, the value of the formal parameter is transmitted back to the actual parameter. To put it in other words - Pass by value-result executes in the same way as pass by value, only difference is when the the function exits - the actual parameter value gets the value of the formal parmeter.
3. 11192
This is the case with when the lvalue is computed at call time, as the value of the formal parameter is transmitted back to the actual parameter when the function exits and returns back to the caller.
In main, function f is called as f(A(1)) (since its A(++k) and k is 0) = > f(1)
int k = 0; A[3] = {0, 1, 2}
void f(int p) { // p=1
cout<<A[k]; // prints 1, since k=1
p = p * 3; // p becomes 3 as initially its 1
cout<<A[k]; // prints 1, since k=1
p = p * 3; // p becomes 9 as initially its 3
cout<<A[k]; // prints 1, since k=1
} // at this point the value of p is transferred to A[1]
// in int main from where it was called. Thus A[1] = 9
int main(){
f(A[++k]); // the value 1 is passed to the function
cout<<A[1]<<A[2]; // prints 9 and 2
}
4. 11192
Same is the case with call by value result when lvalue is computed at runtime, as the value of the formal parameter is transmitted back to the actual parameter when the function exits and returns back to the caller.
int k = 0; A[3] = {0, 1, 2}
void f(int p) { // p=1
cout<<A[k]; // prints 1, since k=1
p = p * 3; // p becomes 3 as initially its 1
cout<<A[k]; // prints 1, since k=1
p = p * 3; // p becomes 9 as initially its 3
cout<<A[k]; // prints 1, since k=1
} // at this point the value of p is transferred to A[1]
// in int main from where it was called. Thus A[1] = 9
int main(){
f(A[++k]); // the value 1 is passed to the function
cout<<A[1]<<A[2]; // prints 9 and 2
}
Let me know of the issues if any, and please give me a like...thank
u....