Code About Us Tutorial


  Functions
    What are Functions?
    Function Definition
    Functions as Parameters to Functions
    Return Values
    Separating Functions
    Passing By Value
    Passing By Reference
    Passing By Pointer
    Inline Functions
    Overloading Functions


   
 

Home > Functions > Passing By Reference

November 27, 2009 2:55 pm

   

Passing By Reference

Again, passing by reference is EXACTLY the same as passing by value, except for the function declaration which goes something like:

 

 
// passing by reference
#include <iostream.h>

// example function that takes a parameter by reference void accelerate(int& speed) { speed = speed + 5; // add 5 to speed }

int main(void) { int vehiclespeed = 10;

cout << "current speed: " << vehiclespeed; // current speed: 10

accelerate(vehiclespeed); cout << "accelerated speed: " << vehiclespeed; // accelerated speed: 15

return 0; }

Passing variables to functions by reference is very usefull if you want to change or update a variable through a function. Also, if your variable is a relatively large variable, or a whole class or struct or array, it is always adviceable to pass the variable by reference since big amounts of data need not be copied when evaluating them.

When you pass by reference, the variable you have passed does not get duplicated, the ampersand (&) in the function definition tells the compiler to pass a reference to the variable's memory location to the funcion. And so, its basically similar to passing by pointer except with a much easier notation and the semantics are a bit different. So in the above example accelerate did not recieve a copy of vehiclespeed, but rather a reference to its memory location and the function was able to alter that value because it had direct access to where it resides in memory.

If you want to add some security to your pass by reference function, what you could do is pass it by a constant reference as you are about to witness.

Passing by a constant Reference

So you want to pass by value and dont want to duplicate your variable eh? Well you have come to the right place. Passing by constant reference is exactly like above except that the function will not be able to alter the variable that you send in.

Take a look at this code:

 

 
// passing by const reference example
#include <iostream.h>

void showItems(const Catalog& cat) { for( int i = 0; i < cat.getNumItems(); i++ ) cat.showItem(i); // calls the show item method of catalog //cat.fillItems(7); // error - could not alter // contents of this class }

class Catalog { private: int nitems; int items[10]; public: Catalog() { nitems = 0; items[0] = 0; } int getNumItems() { return nitems; } void fillItems(int start); void showItem(int item) { cout << "Item " << item << ": " << items[item]; } }

void Catalog::fillItems(int start) { // fill in with some numbers nitems = 10; for( int i = 0; i < nitems; i++) nitems[i] = (i + start + 13) * 3; }

int main(void) { // declare a catalog class and a reference to it Catalog myCatalog; Catalog& myCatalogRef = myCatalog;

myCatalog.fillItems(50);

// print the original copy showItems(myCatalog); // is the EXACT same as saying: showItems(myCatalogRef); // since they are both pointing to the same place

return 0; }

Basically, constant reference means that you cannot alter in any way, the values of the variable.

 

 
// another example of const reference
#include <iostream.h>

void tryAltering(int& byRefVal) { cout << "Before: " << byRefVal; byRefVal++; // try incrementing... // error - you cannot alter constant reference cout << "After: " << byRefVal; }

int main(void) { int somenumber = 99;

tryAltering(somenumber);

return 0; }

// This program will fail because you cannot alter // a constant reference.

Please Rate this Code:


    Comments for: Passing By Reference
Annonymouse User says:
how to pass an array to a function by reference?what will be the exact syntax for that kind of function prototype?

Annonymouse User says:
how to pass an array to a function by reference?what will be the exact syntax for that kind of function prototype? says:
array passing is done by address, not reference

Annonymouse User says:
how to pass an array to a function by reference?what will be the exact syntax for that kind of function prototype? says:
array passing is done by address, not reference says:
Another question is, what happens, if the variable is an instance of a class? Is any of the constructors called?




Add Comment:
Name:
Email:

 «1» THINKQUEST TEAM C0111571 © 2001. All Rights Reserved.