Happy Easter everyone!
I felt today, being a day off, would be a good day to write something up. I’ve had a lot of inquiries lately about the use and functionality of reference operators (&) in variables. Well, basically, in common word, they replace the original variable. Of course, as usual, I have written up a sample code for you all to use, but first I’d like to explain a little bit more about these operators.
When defining a function, you always specify the format of function arguments. Take void test(int arg1,char* arg2); for instance. You have defined your function type “void,” the name “test,” and the arguments “int arg1″ and “char* arg2.” Now, the way these functions are defined, the function will proceed with the values inserted into the function and the function will only apply end values of these variables specifically to this function (unless type other than “void” is defined and you return an output; however, this output would still need to be defined in another function to transfer).
However, if you define a function such as: void test(int& arg1,char& arg2); it actually will append the variables that were used to make the input (thus the “reference”). So let’s take a look at some code, shall we?
/**
* Reference Operator Tutorial
*
* Author: Dennis J. McWherter, Jr.
* (C) 2010 DENNIS J. MCWHERTER, JR.
*
*/
#include <iostream>
#include <cstdlib>
using namespace std;
void deconstruct(float& x,float& y)
{
x/=y;
y*=x;
return;
}
void reconstruct(float& x,float& y)
{
const float z=x;
x*=y/x;
y/=z;
return;
}
int main(int argc,char* argv[])
{
// Define our main variables we'll be using
// Allow users to input!
float a,b;
cout<< "Enter your first number: ";
scanf("%f",&a);
cout<< "Enter your second number: ";
scanf("%f",&b);
cout<< endl << "Output:" << endl << endl;
const float a1=a,b1=b; // Define what our originals were! Please note that this is very recursive
// and can be done much easier with a function returning a value
// But that would ruin the entire point of this demonstration!
// Now we will do our actual functioning
deconstruct(a,b);
printf("%f / %f = %f\n",a1,b1,a);
printf("%f x %f = %f\n\n",b1,a,b);
// Define our "z" variable for here as well
const float z=a,w=b;
// Alright, take everything back to original form
reconstruct(a,b);
printf("%f / %f = %f\n",w,z,b);
printf("%f x %f = %f\n\n",(a1/a1),w,a);
return 0;
}
Now, as you can see, the variables are originally defined by user input. However, when taken to each function their values are changed (as can be seen by the printf values). That is what the purpose of a reference operator is. Now to reconstruct, we can use the same variables with opposite operations. The “const” defined in front of some variables protect them from being “referenced” (precursed with &) so their values will not change after they are defined. And as you can see in the final printf statement, (a1/a1) = 1 which is essentially what happens in the final steps of reconstruct().
As I mentioned, it is very recursive to do it this way (it would be best to simply print an output), but then that would defeat the purpose of this post. Also, for those of you wondering why I used printf(); instead of cout: there is no particular reason. Printf(); in this case (and honestly in most) is simply cleaner and quicker. The thing to remember, however, about printf(); is each function type (in this case %f) has a specific variable type that it can output. %f allows us to print float variables. For a more detailed list, visit http://www.cplusplus.com/reference/clibrary/cstdio/scanf/
Below, you can download the script and a compiled *nix version.
Regards,
Dennis M.
Reference Operators Script