TUTORIAL: Pointer and references in functions.

  • genux
  • Graduate
  • Graduate
  • User avatar
  • Posts: 106
  • Loc: UK

Post 3+ Months Ago

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
Code: [ Select ]
int *pInt =NULL;
int intValue = 55;
pInt = &intValue;
  1. int *pInt =NULL;
  2. int intValue = 55;
  3. 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.
Code: [ Select ]
int normalReturnMethod(int value)
{
  value = value +2;
  return (value);
}

int intValue = 10;

int newValue = normalReturnMethod(intValue);
cout << "new Value : " << newValue << " intValue : " << intValue;
  1. int normalReturnMethod(int value)
  2. {
  3.   value = value +2;
  4.   return (value);
  5. }
  6. int intValue = 10;
  7. int newValue = normalReturnMethod(intValue);
  8. cout << "new Value : " << newValue << " intValue : " << intValue;

with the output being
Code: [ Select ]
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.
Code: [ Select ]
void pointerReturnMethod(int *pValue)
{
  *pValue +=2;
}
int intValue = 10;
pointerReturnMethod(&intValue);
cout << "intValue = " << intValue;
  1. void pointerReturnMethod(int *pValue)
  2. {
  3.   *pValue +=2;
  4. }
  5. int intValue = 10;
  6. pointerReturnMethod(&intValue);
  7. cout << "intValue = " << intValue;

the output of this would be
Code: [ Select ]
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.
Code: [ Select ]
void referenceReturnMethod(int &fValue)
{
  fValue +=4;
}

int intValue = 10;
referenceReturnMethod(intValue);
cout << "intValue = " << intValue;
  1. void referenceReturnMethod(int &fValue)
  2. {
  3.   fValue +=4;
  4. }
  5. int intValue = 10;
  6. referenceReturnMethod(intValue);
  7. cout << "intValue = " << intValue;

and the output would be
Code: [ Select ]
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.

Post Information

  • Total Posts in this topic: 1 post
  • Moderator: Tutorial Writers
  • Users browsing this forum: No registered users and 2 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.