86.0k views
4 votes
Consider the following program written in C syntax:

void fun (int first, int second) {
first += first;
second += second;
} void main() {
int list[2] = {1, 3};
fun(list[0], list[1]);
}

For each of the following parameter-passing methods, what are the values of the list array after execution?
a. Passed by value
b. Passed by reference
c. Passed by value-result

1 Answer

3 votes

Answer

1. Passed by value

list[0] = 1 and list[1] = 3

2. Passed by reference

list[0] = 2 and list[1] = 6

3. Passed by value-effect

list[0] = 2 and list[1] = 6

Step-by-step explanation:

From the code segment above, the points to be noted are

1. An integer array list is declared

2. list is declared to accept only two integer values

3. list[0] = 1

4. list[1] = 3

For pass by value;

None of the actual arguments are changed, so the variables retain the values they were initialized with.

So,

list[0] = 1 and list[1] = 3

Passed by reference

With pass by reference, the arguments are changed.

Given that

list[0] = 1

list[1] = 3

The value of list[0] and list[1] are incremented by their respective original value, as follows

list[0] = 1 + 1 = 2

list[1] = 3 + 3 = 6

So, the new values becomes

list[0] = 2

list[1] = 6

3. Passed by value-effect

The passed by, value-result has the same effect as reference.

User Castletheperson
by
3.7k points