In the following code :
what's the difference between int and int& ?
and what does it mean ??
i got something that n2 and n3 are changed only but not n1 and n4 !! is that related to (int& ) ??

``````#include <iostream>
using namespace std;
void change(int,int&,int&,int)
int main()
{

int n1=99,n2=11,n3=22,n4=88;
change(n1.n2,n3,n4)
cout<<"n1="<<n1<<endl;
cout<<"n2="<<n2<<endl;
cout<<"n3="<<n3<<endl;
cout<<"n4="<<n4<<endl;
return 0;

}
void change(int n4,int& n3,int& n2,int n1)
{
n1=10;n2=20;n3=30;n4=40;
}
``````

What need to know is how things are passed by reference or passed by value

does that mean int ==> passing by reference ..and int& ==> passing by value ???

It's the other way around. `&` means reference; without means copy (value).

## All 6 Replies

does that mean int ==> passing by reference ..and int& ==> passing by value ???

does that mean int ==> passing by reference ..and int& ==> passing by value ???

It's the other way around. `&` means reference; without means copy (value).

ahaaa .. thats why n1 and n4 weren't changed bcuz they just copied as the same their 1st values ..and we neglected n1=10 and n2=40 in the other function `void change` ..

Exactly! Pass by value means the receiving function gets only a copy so changes to it are local only. Pass by reference means that the actual passed object is received by the function, so changes to it are visible outside of the function. It is much like passing a pointer to the object, but safer since you can assume that the reference is valid, vs. a pointer which you need to check for null and such.

thanks alot ..i think i got it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.