0

I am familiar with c but new to c++. What is the difference between below two functions? How the both the programs functionally same? How actually the address is passed in scenario 1 and values are modified without de-referencing the variables.

Scenario 1

    void duplicate (int& a, int& b, int& c)
    {
    a*=2; 
    b*=2;
    c*=2;
    }

    int main ()
    {
    int x=1, y=3, z=7;
    duplicate (x, y, z); 
    cout << "x=" << x << ", y=" << y << ", z=" << z;
    return 0;
    }

Scenario 2

    #include <iostream>
    using namespace std;

    void duplicate (int *a, int *b, int *c) 
    {
    *a*=2;
    *b*=2;
    *c*=2;
    }

    int main ()
    {
    int x=1, y=3, z=7;
    duplicate (&x, &y, &z);
    cout << "x=" << x << ", y=" << y << ", z=" << z << endl;
    return 0;
    }
user1290
  • 19
  • 7

0 Answers0