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

For each of the parameter passing methods what are the values of the list array

ID: 662561 • Letter: F

Question

For each of the parameter passing methods what are the values of the list array after execution?

1- Pass by value

2 -Pass by value result

#include <iostream>

using namespace std;

void fun(int a, int b);

void main()

{

            int list[2] = {1,3};

           

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

           

            cout << "list contains: ";

            for (int i = 0; i < 2; i++)

                        cout << list[i] << " ";

            cout << endl;

}

void fun(int first, int second)

{

            first += first;

            second += second;

}

Explanation / Answer

Firstly, The Questions is somewhat unclear.

         a. You said each of the parameter passing methods. But there is only method with parameters.
         b. "Pass by Value" I understand. I dont think there is anything like "Pass by Value Result"

Now

If the above code is executed, the values of list array won't change. i.e, they remain {1,3}
          Because they are passed by value. In case of pass by value,
copies of the actual elements will be passed to the method and the original data won't get effected.
Or we can also say the changes made in the fun method are local to that method and will go away once we come out of it.
And also the another way of passing is called "Pass by Reference". Here the reference of the original values are passed to the method instead of copies. So any changes inside the fun method affects the values in the main.