Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

3.3 References with Functions

Chapter 2 introduces references as aliases (see "References" on page 41). Although aliases have some advantages over pointers, the real power of references is with functions. This section shows you why references are beneficial with function arguments and return values. We also show you how to use references to pointers in function arguments.


References as Function Arguments

Function arguments may be references. The formats are

 Type function_name(Type &);      // prototype

 Type function_name(Type & arg)     // definition
 {
  function body
 }

Function calls with arguments initialize a reference and create an alias. Inside the body of the function, the alias arg refers to the corresponding argument in the caller's program. Expressions in function statements with alias names as lvalues (such as assignments or increment operators) modify the argument in the caller. Expressions with aliases as rvalues provide read access to function arguments.

References may appear with default arguments only if you initialize them to statics or globals (see "static" on page 125). Here are several examples.

 static double dval = 5.6;         // static
 char f(double & d = 5.6);         // illegal
 char g(double & d = dval);         // legal

Why use references instead of pointers in function arguments? To illustrate, let's write a function called bump(), whose job is to increment its integer argument by one. Our first approach doesn't work, but it tells us what we need.

 inline void bump(int m) {
  ++m;              // increment by one
 }

 int num = 10;
 bump(num);            // call by value
 cout << num << endl;       // displays 10

We don't increment num because we pass it by value to bump() and modify only a local variable (m). Let's try it again with a pointer argument instead.

 inline void bump(int *p) {
  ++*p;            // increment by one
 }

 int num = 10;
 bump(&num);           // call by address
 cout << num << endl;      // displays 11

This time we increment num correctly. We pass num by address (&num) so that bump() can modify it using the compact pointer expression ++*p. (This expression increments what p points to.) Although this works, it's easy to confuse the previous expression with *p++, which compiles but does not increment num! (Here, we increment the pointer and not what it points to; see "Compact Pointer Expressions" on page 63.)

These kinds of mistakes can't happen with references because pointer expressions are unnecessary. Here's the code for bump() with references.

 inline void bump(int & m) {
  ++m;             // increment by one
 }

 int num = 10;
 bump(num);           // call by reference
 cout << num << endl;      // displays 11

This looks like a call by value, except that the function definition makes the compiler pass a reference to bump(), rather than a value. Inside bump(), the compiler generates the code to increment num, using the alias m. Designing bump() with references makes it easy to call (no &) and easy to write (no pointer expressions inside the body of the function).

NOTE

Don't confuse references with pointers in function signatures (int * is not the same as int &). When you pass pointers to functions that expect references (or vice versa), you will get either linkage errors or compilation errors.

Sometimes you need to pass structure types as arguments to functions. We usually do not want to pass these types by value because programs perform poorly when the compiler creates local copies of large structures inside functions. Although function calls with structure pointers would improve performance here, you must use pointer notation (* or ->) to access data members inside your functions. References, on the other hand, let you access data members by using the dot operator (.) and yet perform just as well.

The formats for passing structure types in function prototypes are

 Type function_name(struct_type &);
 Type function_name(const struct_type &);

With constant structure arguments, the compiler reports errors if function_name modifies a data member. Both formats are efficient with struct types because we avoid structure copies during function calls.

The following program illustrates references to structures with function arguments.

Listing 3.8 Constant reference arguments

 // ref.C - const reference arguments
 #include <iostream.h>

 const int MaxBuf = 20;
 struct block {
  char buf[MaxBuf];

  int used;
 };

 int main()
 {
  void display(const block &);      // prototype for display()
  block data;
  int i;

  data.used = 5;
  for (i = 0; i < data.used; i++)     // assign some values
    data.buf[i] = i + 'a'
  display(data);             // call by reference

  data.used = MaxBuf;

  for (i = 0; i < data.used; i++)
    data.buf[i] = i + 'a';
  display(data);             // call by reference
  return 0;
 }

 void display(const block & blockref) {
  for (int i = 0; i < blockref.used; i++)
    cout << blockref.buf[i] << ' ';
  cout << endl;
 }

 $ ref
 a b c d e
 a b c d e f g h i j k l m n o p q r s t

The main() program creates a data structure of type block, and for loops fill its data member array (buf) with characters. Data member used retains a count of the number of valid array elements. The program calls display() twice to print out array elements. Inside display(), we declare blockref a constant reference to block, making calls to display() efficient (no local copies) and safe (no modifications).

NOTE

Use const Type & inside functions signatures that don't modify their structure or union arguments. Judicious use of this idiom makes function design consistent, efficient, and safe.

References as Function Return Types

Functions may return references. The formats are

 Type & function_name(signature);
 const Type & function_name(signature);

The compiler actually returns a pointer in both formats, but a caller does not use pointer notation (*, ->) with the result. In the second format, const makes the compiler report errors if a caller tries to modify the result as an lvalue. Returning references from functions is important with structure types, for the same reason we gave for function arguments earlier: performance. When you return a reference to a structure type from a function call, the compiler returns a pointer instead of a copy.

Another use of references with function returns is cascading. This technique allows you to embed function calls, as follows.

 function_name(function_name(function_name(args))); 

To see why references are appealing with cascading, let's modify our pointer version of bump() from the previous section.

 inline int *bump(int *p) {
  ++*p;                 // increment by one
  return p;               // return pointer
 }

 int num = 10;
 cout << *bump(bump(&num)) << endl;    // displays 12

We embed bump() calls in the cout statement because bump() now returns its first argument, which is a pointer to an integer. We must, however, use & to pass the address of num in the first call to bump() and use * to dereference the return value from the second bump() call. If we forget to dereference (*), the program still compiles and runs, but displays an address (pointer to an integer) instead of the correct answer (12).

References make this simple. Here's the code.

 inline int & bump(int & m) {
  return ++m;               // increment by one
 }

 int num = 10;
 cout << bump(bump(num)) << endl;      // displays 12

With references, the compiler supplies the pointers to pass function arguments and return values. Embedded calls to bump(), therefore, do not require pointer notation.

NOTE

Make sure you do not return dangling references to local, nonstatic variables defined inside functions. Suppose, for example, we modify bump() as follows.

 inline int & bump(const int & m) {            // return reference
  int result = m + 1;                   // local variable
  return result;
 }

This version of bump() returns an incremented value without modifying its argument. We return, however, a reference to a local variable (on the stack), whose memory location no longer exists when the function returns! Fortunately, many C++ compilers warn you about dangling references (and dangling pointers, too). Our machine, for example, displays the following warning message.

 warning: reference to local variable returned 

The correct approach for this version of bump() is to return an integer value, not a reference.

 inline int bump(const int & m) {    // return integer
  return m + 1;
 }

References to Pointers

References may also refer to pointers. The format is

 Type *& name = { init_list };      // reference to pointer 

The brace-enclosed init_list is optional and must contain a pointer expression whose type matches or converts to Type *. When you initialize references to pointers, the braces surrounding init_list are not necessary. Here's an example.

 double d = 3.4;           // d is a double
 double *pd = &d;           // pd is a pointer to a double
 double *& rpd = pd;         // rpd is a reference to a pointer to a double

Spaces are optional between the * and & operators, and arrays of references to pointers are illegal.

Why use references to pointers? Suppose, for example, you want to call a function that exchanges two integer pointers in memory. The following program accomplishes this with a pointer approach using double indirection (**).

Listing 3.9 Exchange pointers, using pointers

 // pxchg1.C - exchange pointers to integers in place using pointers
 #include <iostream.h>

 int main()
 {
  int a = 43, b = 56;
  int *pa = &a, *pb = &b;

  void pxchg(int **, int **);        // double indirect pointers

  cout << *pa << ' ' << *pb << endl;
  pxchg(&pa, &pb);             // pass pointers to pointers
  cout << *pa << ' ' << *pb << endl;
  return 0;
 }

 void pxchg(int **p, int **q) {        // double indirect pointers
  int *pt = *p;               // exchange pointers
  *p = *q;
  *q = pt;
 }

 $ pxchg1
 43 56
 56 43

We pass the addresses of the pointers to pxchg() so the function can swap them in memory. Inside pxchg(), we must be careful to apply single indirection (*) to exchange the pointers.

Here's the approach with references to pointers.

Listing 3.10 Exchange pointers, using references

 // pxchg2.C - exchange pointers to integers in place using references
 #include <iostream.h>

 int main()
 {
  int a = 43, b = 56;
  int *pa = &a, *pb = &b;

  void pxchg(int *&, int *&);      // references to pointers

  cout << *pa << ' ' << *pb << endl;
  pxchg(pa, pb);
  cout << *pa << ' ' << *pb << endl;
  return 0;
 }

 void pxchg(int *& p, int *& q) {     // references to pointers
  int *pt = p;              // exchange pointers
  p = q;
  q = pt;
 }

 $ pxchg2
 43 56
 56 43

The function prototype for pxchg() includes references to integer pointers, so we don't need the address operator (&) with arguments to invoke pxchg(). Similarly, we don't need pointer notation for the function arguments inside pxchg(). As an exercise, try removing the references (&) from this program (but retain each *). You'll discover the program compiles and runs but does not exchange pointers! (Can you explain why not?)

NOTE

Use references to pointers with function arguments instead of pointers to pointers. This approach eliminates pointer notation, simplifies the code, and makes it easier to follow.

  • + Share This
  • 🔖 Save To Your Account