Given the following function: void calc (int a, int& b) { int c; c = a + 2; a = a * 3; b = c + a; } What is the output of the following code segment that invokes calc(): int x = 1; int y = 2; int z = 3; calc(x, y); cout << x << " " << y << " " << z << endl;

Respuesta :

Answer:

The output is 1 6 3

x = 1

y = 6

z = 3

Explanation:

From the code snippet given; x and y is passed into the calc function. z is not passed to the function; so the value of z remain unchanged.

When x and y is passed to the calc function; the code execution as follows:

void calc (int a, int& b) { int c; c = a + 2; a = a * 3; b = c + a; }

void calc(1, 2){ int c; c =1 + 2; x = 1 * 3; y = 3 + 3;}

void calc(1, 2){c = 3; x = 3; y = 6;}

Inside the function, x = 3 but the value of x returned is 1 because of scoping. x is static while y is dynamic.

ACCESS MORE
EDU ACCESS
Universidad de Mexico