(solution) The following program is an example of very poorly used scope

(solution) The following program is an example of very poorly used scope

The following program is an example of very poorly used scope rules. The mixture of local variables, global variables, pass-by-reference, pass-by-value, and repeated variable names makes it difficult to predict what will be printed. Enter the program, without line numbers, then compile and run the program. Save the output to a file using the script command. Edit the output file, by adding text that explains for each line of the output where the variable was declared and where it’s value was assigned, using the line numbers to help. For example, part of the explanation of the first line of output would be: x was declared on line 9 and it was assigned the value 0 on line 9″.

1 #include 
2 using namespace std;
3 int function1(int x, int &y);
4 int function2(int b, int &x);
5 int g = 4;
6 int main() {
7 int a = 1;
8 int b = 5;
9 int x = 0;
10 cout “Main 1: a = ” a ” b = ” b;

11 cout 12 cout 13 {
14 int a = 7;
15 g = 10;
16 cout “Main 2: a = ” a ” b = ” b;

17 cout 18 cout 19 }
20 x = function1(a,b);
21 cout 22 cout 23 cout 24 x = function2(a,b);
25 cout 26 cout 27 cout 28 }
29
30 int function1(int x, int &y) {

31 int a;
32 a = x-y;
33 g = x+y;
34 y = a+g;
35 cout 36 cout 37 cout 38 return a;
39 }
40

41 int function2(int b, int &x) {
42 int temp;
43 temp = b;
44 b = x;
45 x = temp;
46 temp = function1(b,x);
47 g = temp*x;
48 return temp;
49 }