614 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;
}

1 Answer

1 votes
1 votes

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

Related questions

2 votes
2 votes
1 answer
1
0 votes
0 votes
0 answers
2
aimhigh asked Jan 8, 2019
1,260 views
#include<stdio.h>void main(){int p=-8;int i= (p++,++p);printf("%d\n",i);}
0 votes
0 votes
1 answer
3
Rohit Gupta 8 asked Nov 8, 2017
7,112 views
Let A be a square matrix of size n x n. Consider the following program. What is the expected output?C = 100 for(i=0; i<n; i++) for (j=0; j<n; j++) { Temp = A[i][j] + C A[...
2 votes
2 votes
2 answers
4
gaurav9822 asked Aug 29, 2016
737 views
#include <stdio.h int gsum(int *a,int n) { if(n<=0) return 0; else { if(*a%2==0) return *a+gsum(a+1,n-1); else return *a-gsum(a+1,n-1); } } int main(void) { // your code ...