in Programming in C
612 views
0 votes
0 votes
void f(int x,int &y,const int &z)
{x+=z;
    y+=z;
}
int main()
{
    int a=22,b=33,c=44;
    f(a,b,c);
    f(2*a-3,b,c);
    
    printf("a=%d b=%d c=%d",a,b,c);

    return 0;
}
in Programming in C
612 views

1 comment

This code must through an error
0
0

1 Answer

1 vote
1 vote

Note:-func(int &a) means it'll receive the address of the argument being passed when the function is invoked with func(int x) in some caller module(main() function in this case).In other words it is call by reference and any change of "a" inside func() will reflect in the caller module.

Now,initially:-a=22,b=33,c=44(given in the snippet),we need to worry about value of "b" & "c" only,as second and third parameters of the function signature are as highlighted "void f(int x,int &y,const int &z)" and the function is called as "f(int a,int b, int c)".Now as 3rd argument is treated as constant and it's never updated in "f()" we don't need to worry about "c" also,and it'll remain constant throughout.Only "b" will be modified as "y" is updated in "f" function as follows:-

Dry run table
  a b c
Intially 22 33 44
After f(a,b,c) 22 (33+44)=77 44
After f(2*a -3,b,c) 22 (77+44)=121 44

So,a=22,b=121,c=44 is the final output

edited by

1 comment

$77+44=121 $

So answer is 22, 121, 44
1
1

Related questions