Passing Function Parameters by Reference and by Pointer

Earlier I talked about passing objects to functions by value. I said that in the case of structures and classes, it is usually better to pass those objects by reference rather than by value. Any object can be passed by reference. This includes the primitive data types such as int and long, as well as instances of a structure or class. To review, when you pass function parameters by value, a copy of the object is made, and the function works with the copy. When you pass by reference, a pointer to the object is passed and not the object itself. This has two primary implications. First, it means that objects passed by reference can by modified by the function. Second, passing by reference eliminates the overhead of creating a copy of the object.

The fact that an object can be modified by the function is the most important aspect of passing by reference. Take this code, for instance:

void IncrementPosition(int& xPos, int& yPos) {

IncrementPosition(x, y);

// x now equals 21 and y equals 41

Notice that when the function returns, both of the parameters passed have been incremented by one. This is because the function is modifying the actual object via the pointer (remember that a reference is a type of pointer).

Remember that a function can return only one value. By passing parameters by reference you can achieve the effect of a function returning more than one value. The function still only returns one value, but the objects passed by reference are updated, so the function effectively returns multiple values.

As I said, the other reason to pass parameters by reference is to eliminate the overhead of making a copy of the object each time the function is called. When dealing with primitive data types, there is no real overhead involved in making a copy. When dealing with structures and classes, however, the overhead is something to be considered. You should pass structures of any consequence by reference, as the following code demonstrates:

  • structure passed by reference void someFunction(MyStructure& s) {
  • do some stuff with 's' return;

MyStructure myStruct;

someFunction(myStruct);

Notice that the function call looks exactly the same whether the object is being passed by reference or by value.

Do you see a potential problem with passing by reference? If you pass by reference, you avoid the overhead of making a copy of the object, but now the object can be modified by the function. Sometimes you don't want the object to be modified by the function. So what if you want to pass by reference but make sure the object is not modified? Read on and I'll tell you.

The const keyword will allow you to declare a variable as constant.

Once a variable is declared with const, it cannot be changed. The solution, then, is to pass by reference and make the object const:

void someFunction(const MyStruct& s) {

MyStructure myStruct; // later someFunction(myStruct);

Was this article helpful?

+1 0

Post a comment