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

Consider the following program written in C-like syntax: void swap( int a, int b

ID: 666308 • Letter: C

Question

Consider the following program written in C-like syntax:

void swap(int a, int b) {
  int temp;

temp = a;
a = b;
b = temp;
}

int main() {
  int value = 4, list[5] = {4,3,2,1,0};

swap(value,list[0]);
printf("%d and [%d,%d,%d,%d,%d] ",
              value,list[0],list[1],list[2],list[3],list[4]);

swap(list[0],list[1]);
printf("%d and [%d,%d,%d,%d,%d] ",
              value,list[0],list[1],list[2],list[3],list[4]);

swap(value,list[value]);
printf("%d and [%d,%d,%d,%d,%d] ",
              value,list[0],list[1],list[2],list[3],list[4]);

return 0;
}


For each of the following parameter-passing methods, what is the output of the program:

a. Passed by value.
b. Passed by reference.
c. Passed by name.
d. Passed by value-result.

Explanation / Answer

a) output no change in values actually they are not retained

4,4,3,2,1,0

4,4,3,2,1,0

4,4,3,2,1,0

b)Passed by reference means actual values are changed and reflected back

4,4,3,2,1,0

4,3,4,2,1,0

0,3,4,2,1,0

c)Same as that of reference

4,4,3,2,1,0

4,3,2,1,0

0,3,4,2,1,0

d)4,4,3,2,1,0

4,3,2,1,0

0,3,4,2,1,0