Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, Consider the following C-like code fragment: int q = 100; void foo (int j) {

ID: 3610492 • Letter: H

Question

Hi,

Consider the following C-like code fragment:
int q = 100;
void foo (int j) {
int a = 10;
j = j+a;
j = j+q;
return;
}
When we invoke foo(q), what will be the value of q if we usecall-by-value, call-by-value-result,
call-by-reference, and call-by-name?


Here is what I think is right...

Value of q will remain 100 after foo(q).
because call-by-value, any change to the variable in the functionwill not change outside the function. Inside the function the valueof j will be changed to 220 but outside the function the value of qwill remain 100. This I know is right.

value of q will change to 220 afterfoo(q).
if we call by reference theaddress of the variable will be passed to the function. Thsu anychange to the variable inside the function will also change outsidethe function, this I know is right.

These ones I'm a little iffy on...

Call by name
    Value of q will change to 220 afterifoo(q).
   
the original variable is passed so anychange to the variable will also change outside the foo a(int)also

Call by value result
value of q will change to 220 after calling foo(q).
I think this is the same as call by reference (thebook says this) except there won't be any alaling right?


Thanks

Explanation / Answer

int q = 100; void foo (int j) { int a = 10; j = j+a; j = j+q; return; } Call by value:   Value of qwill remain 100 after invoking foo(q). Reason: If wee call-by-value, any change to thevariable in the function will not be persistent outside thefunction. Inside the function the value of j( passing q for j) willbe changed to 220 but outside the function the value of q willremain 100. Call By Reference: Value of q will change to 220after invoking foo(q). Reason: If we call by reference, the address of thevariable will be passed to the function. so any change to thevariable inside the function will also persist outside thefunction. Call By Name: Value of q will change to 220 afterinvoking foo(q). Reason: In this the original variable is passed. thereforeany change to the variable will persist outside the foo a(int)also Call By Value Result: Value of q will change to220 after invoking foo(q). Reason: This is same as call by reference...the onlydifference is there wont be any aliasing in this approach.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote