Determine how each of the following functions will behave Wh

Determine how each of the following functions will behave. What will the following program output to the console? z include using namespace std; int foo (int a, int b, int c) {b = a - c; c = b * a; a = b + c; return a;} int main () {cout

Solution

Output:

x:- 4

y : 0

z :-2

Reason:

when the function foo(1,2,3) is called

int foo(int a,int b,int c) here a=1 b=2 c=3
{
   b=a-c; //b=1-3=-2
   c=b*a; //c=-2*1=-2
   a=b+c; //a=-2+(-2)=-2-2=-4
  
   return a; //here it return a which is -4
}

cout<<\"x: \"<<foo(1,2,3)<<endl; //x: -4
__________
when the function foo(1,-1,1) is called
int foo(int a,int b,int c) here a=1 b=-1 c=1
{
   b=a-c; //b=1-1=0
   c=b*a; //c=0*1=0
   a=b+c; //a=0+0=0
  
   return a; //here it return a which is 0
}

cout<<\"y: \"<<foo(1,-1,1)<<endl; //y: 0

_____________


when the function foo(0,1,2) is called
int foo(int a,int b,int c) here a=0 b=1 c=2
{
   b=a-c; //b=0-2=-2
   c=b*a; //c=-2*0=0
   a=b+c; //a=-2+0=-2
  
   return a; //here it return a which is -2
}

cout<<\"z: \"<<foo(0,1,2)<<endl; //z: -2

__________Thank You

 Determine how each of the following functions will behave. What will the following program output to the console? z include using namespace std; int foo (int a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site