Pasing values by parameters

I want to set values by using a void.

For example

1
2
3
4
5
6
  int M = 5;
  void blah(int p)
  {
    p = 0;
  }
  blah(M);


In my example I want to make M to 0 using blag...
Three ways, one is bad:

1. Make M a global, which you have, and usually this is bad.
2. Use a pointer.
3. Use a reference (this is the best way.)

2. Pointer:

1
2
3
4
5
6
7
8
9

void blah( int* p )
    {
    if( NULL != p )
        *p = 0;

    }
        /*    blah()    */


3. Reference:

1
2
3
4
5
6
7
8

void blah( int& p )
    {
    p = 0;

    }
        /*    blah()    */
Topic archived. No new replies allowed.