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

1. For each of the following parameter passing methods, what are all of the cont

ID: 3537242 • Letter: 1

Question

1.    For each of the following parameter passing methods, what are all of the contents of the variables value and list after each of the three calls to swap?

a)    Pass by value

b)    Pass by reference

c)     Pass by value result

Note: Assume the calls are not accumulative; that is, they are always called with the initialized values of the variables, so their effects are not accumulative.

void main()

{

    int value = 2, list[5] = {1,3,5,7,9};

    swap(value, list[0]);

    swap(list[0], list[1]);

    swap(value, list[value]);

}

void swap(int a, int b)

{

    int temp;

    temp = a;

    a = b;

    b = temp;

}

Assume the calls are not accumulative; that is, they are always called with the initialized values of the variables, so their effects are not accumulative.

Explanation / Answer

A) Pass by value
Call 1: value = 2, list = {1,3,5,7,9}
Call 2: value = 2, list = {1,3,5,7,9}
Call 3: value = 2, list = {1,3,5,7,9}

B) Pass by reference
Call 1: value = 1, list = {2,3,5,7,9}
Call 2: value = 2, list = {3,1,5,7,9}
Call 3: value = 5, list = {1,3,2,7,9}

C) Pass by value-result
Call 1: value = 1, list = {2,3,5,7,9}
Call 2: value = 2, list = {3,1,5,7,9}
Call 3: value = 5, list = {1,3,2,7,9}