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;
    return 0;                                                                                                          

void change(int n4,int& n3,int& n2,int n1)

Edited 1 Year Ago by rose_2

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.

This article has been dead for over six months. Start a new discussion instead.