0

I so confused about parameter types in C++ I have two functions in following code:

void degistir2( int *x, int *y )
{ 
   int gecici;
   gecici = *x; *x = *y;
   *y = gecici;
}
void degistir3( int &x, int &y )
{ 
    int gecici;
    gecici = x; x = y;
    y = gecici;
}

What is the difference of these functions? I know the pointers and references but I don't know how it works in above functions.

Çağrı Can Bozkurt
  • 653
  • 1
  • 10
  • 17

1 Answers1

1

In your first example, your function is given a copy of the address of x and y.

In the second example, your function is given the same instance of x and y as the code that function call originates from.

By default, functions receive a copy of the variable being passed to the function. Both of your examples allow you to access the original x and y by different methods.

See 7.2-7.4 of this guide for more details and examples.

Matthew Pope
  • 7,212
  • 1
  • 28
  • 49