I think this is an easy question but I don't know the answer to it. How does one link the value of an argument to a function and the functions parameters? For instance:

int a;
int b;
void fn(int &c, int &d)
{
     c++;
     d++;
}

int main()
{
     fn(a,b);
     cout << "A:" << a << " B:" << b << endl;
     return 0;
}

Desired result : "A:1 B:1"
How do I link a with c and b with d (only when I make the function call)?

(Nevermind, problem resolved) How do I delete a post?

Recommended Answers

All 2 Replies

The parameters are mapped in the order in which they appear in the argument list. The first parameter on line 11 (a) is mapped to the first parameter on line 3. The second parameter on line 11 (b) is mapped to the second parameter on line 3. That mapping can go on almost indefinitely.

In above coding you make the refrence to the both integers a and b, when the function is called from line 11 the control reaches to line 3 to read the definition of function and two thins happen:

1: int &c=a;
2: int &d=b;

It means c is reference variable to a and d to b.
Now any change in c and d will effect change in a and b respectively.

Note: In line no.3 if you avoid use of & operator then change in c and d will not
change in a and b respetively that may be logocal error in programe

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.