void pxc(int& c, int& d)
{
int k = c;
c = d;
d = k;
}
int main()
{
int a = 15, b = 100;
pxc(a, b);
}
Solution :
void pxc(int& c, int& d) // here c & d represent a & b if changed value in c , d change the actual value in a , b
{
int k = c; // store the value of c in variable k
c = d; // store the value of d in variable c
d = k; // store the value of k in variable d
} // It's actually swapping values of the two variables
int main()
{
int a = 15, b = 100; // initialize the value of a and b
pxc(a, b); // here after calling the function a= 100 and b= 15
}
Note : reference variable represent the same variable, with other name. It's just like represent same variable with two names. If we change value in reference variable, it will change the actual value in original variable.
Get Answers For Free
Most questions answered within 1 hours.