Question: Consider the following program. Assume the program compiles and runs. #include using namespace std; void test(int &, int); int z = 0; int main() {

  1. Consider the following program. Assume the program compiles and runs.

#include

using namespace std;

void test(int &, int);

int z = 0;

int main()

{

int count, a, b;

z = 12;

a = 3;

b = 2;

for (count = 1; count <= 2; count++)

test(a, b);

cout << In main a = << a << b = << b << z = << z << endl;

return 0;

}

void test(int& y, int c)

{

static int x = 0;

y = 10;

z = z + 1;

x = x + 2;

y = y + 1;

c = c + 1;

cout << "Inside test x = " << x << " and y = "

<< y << and z = << z << and c = << c << endl;

}

Answer the following questions:

  1. What is the output? Consider variable scope.
  2. Considering the function test, parameter 1 is called by reference. What is passed into the function for parameter 1, i.e., what does parameter 1 receive?parameter 1 receives is pass by reference argument. This type of argument shares reference of the main variable. In this case if argument value changed inside method, it will reflect on the main variable
  3. Again, considering the function test, parameter 2 is called by value. What occurs in memory for parameter 2 and local variable int x? Hint: consider memory, parameters and local/static variables passing by value. here content of the variable passed as argument. so in this case if argument value changed inside method, main variable will be unchanged.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!