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
*/
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment