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;
}

Dani AI

Generated

Short answer: int as a parameter is pass-by-value (a copy), int& is pass-by-reference (an alias to the caller’s object). As noted, & means reference; as added, it behaves like passing a pointer but without null/indirection syntax.

The reason only two variables changed in your run is positional: the function’s parameter list decides which argument is copied and which is referenced by position, not by the names you use inside the function. For example, with a call
change(n1, n2, n3, n4) and a signature
change(int p1, int& p2, int& p3, int p4),
only p2 and p3 are aliases to the caller (so changes to them affect n2 and n3). p1 and p4 are local copies and do not change the caller’s n1/n4. If the function definition renames parameters (for example int n4, int& n3, int& n2, int n1) the positions still map the same way, which can look confusing.

Two practical notes:

  • The posted snippet has a couple of typos that prevent compilation (missing semicolon after the prototype and the call looks like change(n1.n2,...) instead of change(n1, n2, n3, n4);). Fix those first.
  • References must bind to lvalues (you cannot bind a non-const int& to a temporary like 5). Use references when you want to mutate the caller’s object and pointers when you need reseating or nullable semantics.

Small illustrative example:

void f(int v, int& r) { v = 1; r = 2; } // v is a copy, r aliases caller

int a = 0, b = 0;
f(a, b); // a == 0, b == 2

That demonstrates the behavior you observed and why only the reference parameters changed.

Recommended Answers

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.