R E F E R E N C E     V A R I A B L E S

PART 1: The Easy Stuff
When declaring a reference variable you must also make it refer to something at the same time. To declare on you simply make a variable of the type you are going to be referring to, make up your own name, and put an ampersand (&) in front of it:

int &ref;

That creates a reference variable called ref of type integer. Of course this doesn't do anything because you can't assign references to other variables at any other time than declaration. So to make ref refer to something we have to do it in the declaration:

int &ref = x;

This is all assuming we have a variable called x and that it is also an integer (int). But after doing this, anything we do to ref will effect x. Try this source code (cut and paste).


/* -example-source-1------------------------------------------------- */

#include <iostream.h>

void main()
{
  int x = 10;         // create integer variable called x
  int &ref = x;       // make a reference variable that refers to x

  cout << "x is " << x << " and ref is " << ref << endl;

  cout << "Now we change ref to equal 25 ... " << endl;
  ref = 25;
  cout << "And now x is " << x << " and ref is " << ref << endl;
}

/* ------------------------------------------------------------------ */
  
What I have explained so far with reference variables is actually fairly simple then. Just remember these rules:

If you don't understand to the point I have gotten right now then ask questions. Because this is the easy part... it gets, um difficult from here (in comparison).

PART 2: Using Reference Variables in Functions
The previous section was an intro into reference variables. But to be quite honest if you use them like that then they're not really necessary. Reference variables come into shine when it comes to functions.

The point of reference variables and functions is that you can pass a variable as a parameter and have the variable changed in the function. Like in the following code snippet.

/* -example-source-2------------------------------------------------- */

#include <iostream.h>

void times2(int &x);  // function prototype

void main()
{
  int var;            // declare var as integer variable
  var = 10;           // put value of 10 in var
  cout << "var is " << var << endl;

  times2(var);        // call 'times2()' with var as parameter
  cout << "var is now " << var << endl;
}

void times2(int &x)
{
  x = x * 2;
}

/* ------------------------------------------------------------------ */
  
In the above example, whatever we did to x in the function 'times2()' would effect the actual variable we passed to the function. In this case we multiplied x by 2. We passed var to times2(). So x becomes a reference to var when we called it. Now we make x equal itself times 2. And since we passed var to the function it multiplies THAT by two.

But, you're probably thinking (maybe), you could just have times2() return a value. However in returning you can only get that one value from the function. With references you could get multiple values, like in the following.

/* -example-source-3------------------------------------------------- */

#include <iostream.h>

void times2(int &v1, int &v2);  // function prototype

void main()
{
  int x,y;
  x = 10;
  y = 15;
  cout << "x is " << x << " and y is " << y << endl;

  times2(x,y);

  cout << "x is now " << x << " and y is now " << y << endl;
}

void times2(int &v1, int &v2)
{
  v1 = v1 * 2;
  v2 = v2 * 2;
}

/* ------------------------------------------------------------------ */
  

Mara Hong to see the difference between passing by reference and passing by value tried the above program but took out all the ampersands. I thought I'd post that below as well. Because you can see that when you don't pass by reference, any modifications made to the parameters (v1 and v2 in this example) will not effect what you pass to the function (x and y for this case). Why? Because without passing by reference you're simply telling v1 and v2 to contain values. Anyway, try out the source, you'll see. (I added an extra line too, if you noticed):


/* -example-source-4-------------------------------------(Mara-Hong)- */

#include <iostream.h>

void times2(int v1, int v2);  // function prototype

void main()
{
  int x,y;
  x = 10;
  y = 15;
  cout << "x is " << x << " and y is " << y << endl;

  times2(x,y);

  cout << "x is now " << x << " and y is now " << y << endl;
}

void times2(int v1, int v2)
{
  v1 = v1 * 2;
  v2 = v2 * 2;
  cout << "v1 is " << v1 << " and v2 is " << v2 << endl;
}

/* ------------------------------------------------------------------ */
  


The End (for now, *muh ha ha ha*)

Page Content & Design ©1998
By Neil C. Obremski

home

prfce

loop

refs

ptrs

struct

class

array

rsrc

site

indx
Wed May 12 21:45:26 1999
C O N T A C T   F O R M
Name:
E-Mail:

 
 
Message:
 
 
Newsletter: (un)subscribe from my newsletter
C O N T A C T
You may use the form to your left to contact me if you have unanswered/unsaid questions, complaints, contributions, comments or just want to bitch. I recently made guidelines to follow before writing me. Please respect these, thank you.

Ashrak DeadEye nobremski@hotmail.com
home 652447
╧ceGoblin icegoblin@hotmail.com
school 5676542
Masta masta@cyclone7.com
business none