Question & Answer: Can someone explain me how this work in details please……

Can someone explain me how this work in details please.

I know the answers. 13,5,5,6

#include <iostream>

#include <cstdlib>

using namespace std;

int& bar(int& b){

b–;

return b;

}

int foo(int* a){

*a = *a+3;

*a+2;

return 5;

}

int bazz(int c){

c = c+2;

return c;

}

int main(){

int w = 10;

int x = foo(&w);

int& y =bar(x);

int z = bazz(y);

x++;

cout<<“w: “<<w<<endl;

cout<<“x: “<<x<<endl;

cout<<“y: “<<y<<endl;

cout<<“z: “<<z<<endl;

}

Expert Answer

 

Explanation is in comments

I find it easier to explain with comments rather than paragraph

#include <iostream>

#include <cstdlib>

using namespace std;

int& bar(int& b){

b–; // do a post decrement operation that is current value of variable is used now and whenever this variable

// will be used in future its decremented value will be used

return b; // this function simply return the value which was passed to it

}

int foo(int* a){ // a variable is passed

*a = *a+3; // that passed variable gets an increment of 3

*a+2; // this line does nothing as it is not assigning any value to any variable

return 5; // and then it returns 5

}

int bazz(int c){

c = c+2; //just adds 2 to the argument passed and return it

return c;

}

int main(){

int w = 10; // assigns 10 to w

int x = foo(&w); // calls foo() on w that do some manipulation to w and changes it to 13 and assigns 5 to variable x

int& y =bar(x); // calls bar() on x that did not change x and assign the value of x to y

int z = bazz(y); // calls bazz() on y that just adds 2 to y and assign the value to z

x++; // Do a post increment on x

cout<<“w: “<<w<<endl; // prints the value of w on screen

cout<<“x: “<<x<<endl; // prints the value of x on screen

cout<<“y: “<<y<<endl; // prints the value of y on screen

cout<<“z: “<<z<<endl; // prints the value of z on screen

}

———————————————————————–

EXPLANATION # 2

——————————————————————–

YOU CAN UNDERSTAND THIS CODE OR ANY OTHER CODE SIMPLY BY ADDING COUT STATEMENTS

It becomes easier to understand when you know at each step that what is happening like this

#include <iostream>

#include <cstdlib>

using namespace std;

int& bar(int& b){

cout<<“Start bar -> “<<b;

b–;

return b;

}

int foo(int* a){

cout<<“start foo- > “<<*a;

*a = *a+3;

cout<<“middle foo- >”<<*a;

*a+2;

cout<<“end foo –> “<<*a;

return 5;

}

int bazz(int c){

cout<<“Start bazz -> “<<c;

c = c+2;

cout<<“end bazz -> “<<c;

return c;

}

int main(){

int w = 10;

int x = foo(&w);

int& y =bar(x);

int z = bazz(y);

x++;

cout<<endl;

cout<<“w: “<<w<<endl;

cout<<“x: “<<x<<endl;

cout<<“y: “<<y<<endl;

cout<<“z: “<<z<<endl;

}

Still stressed from student homework?
Get quality assistance from academic writers!