Introduction

On the tail end of my previous tutorial here, this is the tutorial about passing pointers and references to functions. A function is just a set structure of code that can be called time and time again.

Review of a pointer and reference

A pointer is a value of an memory location, and a reference (address) is the memory location of a variable. For example

int *pInt =NULL;
int intValue = 55;
pInt = &intValue;

The pInt is a pointer which its variable is also a memory location, but its value is first set to NULL (you never know if the value will be something else and you do not want to point to a not know area in memory) and then a normal variable intValue is set a value of 55. The last part is that the pointer pInt has its value set to a memory location of intValue (via the & reference/addresss symbol).

Normal copy-value-of

The normal way to pass a variable to a function is that the parameter of the function is passed a copy of the variable and the function acts on the copy. e.g.

int normalReturnMethod(int value)
{
    value = value +2;
    return (value);
}

int intValue = 10;

int newValue = normalReturnMethod(intValue);
cout << "new Value :  " << newValue << " intValue  : " << intValue;

with the output being

new Value = 12 int Value : 10

the value in the function parameter is just a copy of the original variable and cannot effect the original variable because it has its own memory location in the memory address space of the program. Which is a good thing.

Passing a pointer (memory location) to a function

When you are passing a memory location in the parameters to a function the parameter will actually still point to the variable that has been passed and thus any alterations done to that parameter variable will have a direct result on the data stored that was passed. e.g.

void pointerReturnMethod(int *pValue)
{
    *pValue +=2;
}
int intValue = 10;
pointerReturnMethod(&intValue);
cout << "intValue = " << intValue;

the output of this would be

intValue = 12

because you are passing in the memory location and thus the *pValue+=2 will increment the intValue data.

Reference parameter in a function

The next one is a reference (&) parameter and these are the fun ones, because you can pass in a variable just the copy-value-of function but it will actual act like a pointer function parameter, because it is still acting on the data of the passing variable. e.g.

void referenceReturnMethod(int &fValue)
{
    fValue +=4;
}

int intValue = 10;
referenceReturnMethod(intValue);
cout << "intValue = " << intValue;

and the output would be

intValue = 14

which can cause some issues when you are debugging, if you thought that a parameter passed was not being "accessed" by the function but it was then what you thought the variable should be before and after the function may not be.

Conclusion

If you are going to pass parameters to a function, in the function make sure that either you inform the developer that it may act on the value e.g. in the function name, or make the parameter a const (constant) type so that it will not effected within the function. This will be my next tutorial, about the 3 places that you can have a const (constant) in a function declaration and what they mean.

I do really like to have any feedback regarding any tutorial/post, just reply or PM me.. glad to help, better to share knowledge.

This page was published on It was last revised on

0

0 Comments

  • Votes
  • Oldest
  • Latest