34 1. Determine all the output from the following segment of program as it would
ID: 3772680 • Letter: 3
Question
34 1. Determine all the output from the following segment of program as it would appear on the screen. (11 points) 2. Explain the following lines: 2-3, 8-9,10,11-12,13-14 (11 points) int fa(); void fb(int = 5); float fc(int &); int main() { int x = 5, y; float z; y = fa(); cout << "y: " << y << endl; fb(); z = fc(x); cout << "z: " << z << endl; fb(x); cout << "x: " << x << endl; return 0; } int fa() { int x = 1; x += 5; return x; } void fb(int y) { static int x = 4; x -= y; cout << "x:" << x * x << endl; } float fc(int & x) { x += 2; cout << "x:" << x << endl; return 3.14; }
Explanation / Answer
Output :
y: 6
x:1
x:7
z: 3.14
x:64
x: 7
Explanation :
int fa();
void fb(int = 5); // Default arguments : Allows a function to be called without providing one or more trailing arguments.
float fc(int &); // Pass by reference , function declaration
int main()
{
int x = 5, y;
float z;
y = fa(); // fa() called and the value returned is assgined to y
cout << "y: " << y << endl;
fb(); // fb() called without any arguments , but default value 5 is passed as its argument
z = fc(x); //fc(x) is called and value returned is assigned to z
cout << "z: " << z << endl;
fb(x); // when this is called second time , the value of x in the definition will be -1.
cout << "x: " << x << endl;
return 0;
}
int fa()
{
int x = 1;
x += 5;
return x;
}
void fb(int y)
{
static int x = 4; // static x , hence the lifetime of this variable is program
x -= y;
cout << "x:" << x * x << endl;
}
float fc(int & x) //By reference
{
x += 2;
cout << "x:" << x << endl;
return 3.14;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.