0

Like in this code, what I don't understand is how 'n1' [ increment(n1) ] is being passed to 'number& n2'[void increment(number& n2)]. how can we pass n1 into &n2? Please let me know if I am getting the basics wrong, because its only recently that i have started to learn C and C++.

// C++ program to pass structure as an argument
// to the functions using Call By Reference Method

#include <bits/stdc++.h>
using namespace std;

struct number {
    int n;
};

// Accepts structure as an argument
// using call by reference method
void increment(number& n2)
{
    n2.n++;
}

void initializeFunction()
{
    number n1;

    // assigning value to n
    n1.n = 10;

    cout << " number before calling "
        << "increment function:"
        << n1.n << endl;

    // calling increment function
    increment(n1);

    cout << "number after calling"
        << " increment function:" << n1.n;
}

// Driver code
int main()
{
    // Calling function to do required task
    initializeFunction();

    return 0;
  • 6
    While [`using namespace std` is a bad habit](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) and often considered okay for small examples, [including *any* `bits` header file is just plain wrong](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – Some programmer dude Feb 03 '22 at 07:14
  • As for your question, I'll guess most compilers implement references as a kind of syntactic sugar for pointers. I.e. the generated assembly code will pass a pointer to `n1`. – Some programmer dude Feb 03 '22 at 07:17
  • 2
    The line `increment(n1);` calls the function `increment`, and passes `n1` as the argument. That means that for that specific invocation of `increment`, its value of `n2` is a reference to `n1`. – Nathan Pierson Feb 03 '22 at 07:18
  • 2
    Perhaps the problem is understanding references in general? Then look at a reference as an *alias* of something else. If, instead of calling `increment(n1)`, you had `number& n2 = n1; n2.n++;`, then `n2` is an *alias* of `n1`. Every time you use `n2` after that, you really use `n1`. – Some programmer dude Feb 03 '22 at 07:25

1 Answers1

2

Since you are taking the parameter to increment function as a refrence so when you pass n1 to it as follows:

increment(n1);

It is passed as a reference to the increment function which basically means that alias of n1 is created with the name n2 void increment(number& n2) This means whenever there is a change in n2, It is also reflected in n1(Because they are refering to the same location)

Also, passing a variable as Reference means allowing a function to modify a variable without creating a copy of it by declaring reference variables. The memory location of the passed variable and parameter is the same and therefore, any change to the parameter reflects in the variable itself.

So, after the function call: n1.n is incremented

  • 1
    Got it...Just went through this concept of reference variables(alias)....thanks for your help.....also, according to what i read, one thing that I would like to mention here is they are not pointing to same location but are sharing the same location( cause only pointers can point to memory location not reference variables)...please correct me if I am wrong. – Shubham Patil Feb 03 '22 at 11:03