Sunday, July 12, 2009

Can someone explain pass by reference functions in simple detail if possible and include a code example 4 C++?

Pass by value makes a copy of the variable parameter(s) and /pass by reference uses the address of the variable so it's the real thing and not a copy being used.





//pass by value


void alter(int a)


{


a++;


}


int main()


{


int x = 5;


cout %26lt;%26lt; x %26lt;%26lt; endl;


alter(x);


cout %26lt;%26lt; x %26lt;%26lt; endl;


return 0;


}


/*output


5


5


*/


-------- ----------------


// pass by reference





void alter(int%26amp; a)


{


a++;


}


int main()


{


int x = 5;


cout %26lt;%26lt; x %26lt;%26lt; endl;


alter(x);


cout %26lt;%26lt; x %26lt;%26lt; endl;


return 0;


}


/*output


5


6


*/


No comments:

Post a Comment