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

Consider the following code: int x = 0; int main() { foo(x); print x; //print th

ID: 3578816 • Letter: C

Question

Consider the following code:

        int x = 0;

        int main() {

          foo(x);

          print x; //print the value of x

        }

        void foo(int a) {

          x = 3;

          a++;

        }

1) What will this code print if parameters are passed (a) by value, (b) by value-result (c) by reference? Be sure to justify your answers.

2) Briefly summarize the semantic and implementation tradeoffs between pass-by-reference and pass-by-value-result.

3) Think of a new program that would behave differently depending on whether a certain parameter is passed by reference or by value-result, describe the program using real code.

Explanation / Answer

1) The code prints 3 if passed by value, 4 if passed by reference and 4 if passed by value-result.
2) A pass by reference might be problametic in partitioned memory as reference variable might rest in a different memory and might lead to a page fault. Pass-by-result in better these situtaions. But when there is concurrent execution pass-by-value-result may give unpredictable values.
3)
void func(int x,int y)
{
   x=1;
   y=2;
   return;
}
int main()
{
   int b=0;
   func(b,b);
   printf("%d ",b);  
   return 0;
}
Here the value of b can be 1 or 2, based on which value is returned first. But this is unpredicatable.

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