Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Consider the following program. Assume the program compiles and runs. #include using namespace std; void test(int &, int); int z = 0; int main() {
- 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:
- What is the output? Consider variable scope.
- 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
- 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started