*Reference variables & parameters --------------------------------- In C++ it is posible to have more than 1 variable all sharing the same location in memory. For example we could have another variable which also uses the same location in memory. Such a variable is called a reference variable because it references or accesses the same value as another variable. ------------- int number; //the original variable number = 16; //store a value int &ref_num = number; //create a reference to the variable ------------- The use of the &, means that the variable ref_num is a reference to the variable number. The two names ref_num and number both use the same memory location, and so control the same data. ------------- cout << number << endl; cout << ref_num << endl; //both lines print 16 num +=10; //increase by 10 cout << number << endl; cout << ref_num << endl; ref_num *=2; cout << number << endl; cout << ref_num << endl; ------------- *Parameters in functions (passing by value) ------------------------------------------- The C++ and C languages were written so that the default way in which parameters are passed to functions, is passed by value. This means that when a function is called, the value (only) of the actual parameters is passed to the function. In other words, the function has has to make a local copy of the data in a seperate memory location and can only work on the copy. ------------- #include void mystery(int num); void main(void) { int a = 6; cout << "the value of A is " << a << endl; mystery(a); cout << "the value of a is " << a << endl; } void mystery(int num) { cout << "the value of num is " << num << endl; num = 82; cout << "the value of num is " << num << endl; } *Passing by reference --------------------- #include void mystery(int &num); void main(void) { int a = 6; cout << "the value of a is " << a << endl; mystery(a); cout << "the value of a is " << a << endl; } void mystery(int &num) { cout << "the value of num is " << num << endl; num = 82; cout << "the value of num is " << num << endl; } //////////////////////////////////////////////////////////////// Write a function which accepts a floating point parameter (by reference) and doubles that parameter. ------------------------------- #include void double_num(float &num); void main(void) { float number; cout <<"enter a number "; cin >> number; double_num(number); cout << "the number is " << number << endl; } void double_num(float &num) { cout << "the number is " << num << endl; num *=2; }