Function Home Work


What is the output of the following programs? (Some contain lines that will cause errors when you try to compile -- cross out these lines or parts of lines, and trace the program without them.) Show your work!! For every program draw the memory locations, lable locations with variable names, and show how each location changes as the program executes!!

Assume #include<iostream> is in each program, assume no semicolon or comma errors.


a.

int f(int a, int b)
{
a++;
b++;
a++;
cout << a << b << endl;
return b;
}

int main()
{
int i = 3; j = 5;
f(i,j);
cout << i << j << endl;
int k = f(i,j);
cout << i << j << k << endl;
return 0;
}
b.

int f(int a, int& b)
{
a++;
b++;
i++;
j++;
cout << a << b << i << j << endl;
return a+2*b;
}

int main()
{
int i = 9; j = 3;
int k = f(i,j);
cout << i << j << k << a << b << endl;
j = f(i,k);
cout << i << j << k << endl;
return 0;
}
c.

int f(int a)

a--;
return 2*a;
}
int g(int &a)
{
int c = 7;
int d = f(c);
cout << a << c << d << endl;
return a+c+d;
}
int main()
{
int a = 5; b = 10;
int c = f(a);
int d = g(b);
cout << a << b << c << d << endl;
}