<< Chapter < Page Chapter >> Page >
An introduction and example of parameter passing by reference as used within the C++ programming language.

Overview

When we pass parameters to functions we usually pass by value; that is the calling function provides several values to the called function as needed. The called function takes these values which have local scope and stores them on the stack using them as needed for whatever processing the functions accomplishes. This is the preferred method when calling user defined specific task functions. The called function passes back a single value as the return item if needed. This has the advantage of a closed communications model with everything being neatly passed in as values and any needed item returned back as a parameter.

By necessity there are two exceptions to this closed communications model:

  1. When we need more than one item of information returned by the function
  2. When a copy of an argument cannot reasonably or correctly be made (example: file stream objects).

These exceptions are handled by parameter passing by reference instead of passing a value. The item passed is called a reference variable and it represents a concept of an alias for the variable. Any change made to the reference variable is actually performed on the variable that it represents. The symbol of the ampersand is used to designate the reference variable (and it is associated with the address operator).

Parameter passing by reference

// prototype void process_values(int qty_dimes, int qty_quarters, double&value_dimes, double&value_quarters); // variable definitionsint dimes = 45; int quarters = 33;double value_dimes; double value_quarters;// somewhere in the function main process_values(dimes, quarters, value_dimes, value_quarters);// definition of the function void process_values(int qty_dimes, int qty_quarters, double&value_dimes, double&value_quarters); {value_dimes = dimes * 0.10; value_quarters = quarters * 0.25;}

Got questions? Get instant answers now!
The ampersand must appear in both the prototype and the function definition but it does not appear in the function call.

The above example shows the basic mechanics of parameter passing by reference. You should study the demonstration program in conjunction with this module.

Demonstration program in c++

Creating a folder or sub-folder for source code files

Depending on your compiler/IDE, you should decide where to download and store source code files for processing. Prudence dictates that you create these folders as needed prior to downloading source code files. A suggested sub-folder for the Bloodshed Dev-C++ 5 compiler/IDE might be named:

  • Demo_Programs

If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.

Download the demo program

Download and store the following file(s) to your storage device in the appropriate folder(s). Following the methods of your compiler/IDE, compile and run the program(s). Study the source code file(s) in conjunction with other learning materials. You may need to right click on the link and select "Save Target As" in order to download the file.

Download from Connexions: Demo_Parameter_Passing.cpp

Definitions

reference variable
Used with parameter passing by reference.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals - a modular structured approach using c++. OpenStax CNX. Jan 10, 2013 Download for free at http://cnx.org/content/col10621/1.22
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals - a modular structured approach using c++' conversation and receive update notifications?

Ask