I have a homework assignment where I'm supposed to use pass by references. I'm reading my text and don't really get it. The assignment is to create a program that converts Fahrenheit to Celsius and vise versa. It's also supposed to contain a print function that will output the results while using the pass by references. If anyone can look at my code and point me in the right direction, i'd really appreciate it.

#include <iostream>
using namespace std;

void menu();
void ftoc(int f);
void ctof(int c);

int main()
{
    int fahrenheit;
    int celsius;
    int option;
    do
    {
     menu();
     cin >> option;
     cout << endl;
     switch (option)
     {
    case 1:
        ftoc(fahrenheit);
        break;
    case 2:
        ctof(celsius);
        break;

    case 99:
        break;

    default:
        cout << "Invalid Entry. Try Again" << endl <<endl;
        }
    }
    while (option !=99);
    return 0;
}
void menu()
{
    cout << "Enter--" << endl;
    cout << "1: Convert Fahrenheit to Celsius" << endl;
    cout << "2: Convert Celsius to Fahrenheit" << endl;
    cout << "99: To quit the program" << endl;

    }

void ftoc(int)
{
cout << "Fahrenheit To Celsius Converter. ";
int fahren;
cout << "Enter Fahrenheit Temperature: ";
cin >> fahren;
int celsius;
celsius = (fahren-32)*5/9;
cout << fahren << " Degrees Fahrenheit =  " << celsius << " Degrees Celsius " <<endl <<endl;
    }

void ctof(int)
{
cout << "Celsius to Fahrenheit Converter. ";
int celsius;
cout << "Enter Celsius Temperature: ";
cin >> celsius;
int fahren;
fahren = (celsius*9/5)+32;
cout << celsius << " Degrees Celsius = " << fahren << "  Degrees Fahrenheit " << endl << endl;
    }

Recommended Answers

All 5 Replies

I have a homework assignment where I'm supposed to use pass by references. I'm reading my text and don't really get it.

I think part of the problem is that you aren't clear on how you pass ANYTHING to a function. I will show you why you need to pass to functions, then show you how references work.

First of all, variables have scope. That means they are only available for use within their local area. Outside their local area, they are unavailable. For example:

int main()
{
int w = 0;

   {
   int x = 0;   //x only exists inside these braces - it's own local area.
   cout  << x;
   cout << w;
   }

cout << x;   // This would cause a complier error " undeclared indentfier "x".
cout << w:   // The local area for w is the two braces for the main() function so
                //    it is available throughout the main() function (but not outside
                //    it!).
return 0;

}

The local area for any variable is the two nearest braces { } in which the variable is declared. Outside those braces it is not available. For x, it's the two closest braces. For w, it's the whole main() function.

No function can be declared inside of another function - they must be declared outside in "global space". Since any function that you write will be outside the braces of the main() function, it will not be able to see or touch any variables within the main() function. So, if your function needs to use any data that's over there in the main() function, then that data (variables, etc.,) need to be passed all the way over to your function. There are different ways in which variables are passed, but we will look at only two of them now - "pass by value" and "pass by reference".

#include<iostream>
using namespace std:

int foodo(int, double); //just a prototype to let the complier know ahead of time that
                        //                                      such a function exists.

int main()
{
int sum = 0;
int x = 0;
double y = 0;

++x;
++y;

sum = foodo(x,y);       //we are sending two values, one an int and the other a double
                        //       to our foodo() function somewhere out there in global
                        //       space and expecting something back to be put in "sum".
cout << sum;

return 0;

}

// Now we are declaring our foodo() function out here in global space.

int foodo(int NUMBER1, double NUMBER2)
{
cout << NUMBER1;        //NUMBER1 "catches" the first value passed to here from main()  
cout << NUMBER2;        //         which is x (and it has to be an int) while NUMBER2  
NUMBER1 = NUMBER1 + 5;  //         catches the second value passed which is y (and it  
                        //         has to be a double). 

return NUMBER1;         //The value of NUMBER1 is passed back to the main() function
                        //         and is "caught" by the variable "sum (and it has to
                        //         be an int of course).
}

Now look how you did your ftoc() and your ctof() functions - you passed a variable to each and didn't even use them!

Anyway, there is one point worth noting here, and that is that when we pass values to functions like we did above, we are only passing COPIES of those variables. So then, whatever we do in our foodo() function to those values has no effect on the orignal values back in main(). They all remain unchanged. It would be like if I gave you a copy of my paycheck. You could tear it up all you want and I would'nt care because it is only a copy. My original paycheck remains unscathed. And so it is when we pass variables by value.

But what if we wanted to actually change the value of a variable back in main() from our function which is way out yonder? Well, we could use a variable of type "voodoo". You know, like those voodoo dolls. Like if you have a voodoo doll that represented your dad, and you stick a needle in the dolls arm, and then your dad who is all the way across town jumps when he feels the the needle prick! That's what a reference is, it's like a voodoo variable for some other variable. So, if we want to mess with some variable over there in the main() function, we just make us a voodoo variable and use that in our function.

Of course, it is not actually called a voodoo variable, and there is no type voodoo either - I was just using that for illustrative purposes. But it is called a reference, and when we pass a reference to a function, we can actually mess with a variable that would normally be unreachable from our function. Here is how you declare a reference:

#include<iostream>
using namespace std:

void foodo(double&)          //prototype for our foodo() function.

int main()
{
int x = 0;
float y = 0.0;
double z = 0.0;
     

int& voodoo = x;     // voodoo is now a reference for x;
float& doovoo = y;   // doovoo is now a reference for y;
double& count = z;   // count is now a reference for z;

double a = 777.125; 
foodo(a);            // call our foodo() function to print out the value of a.

return 0;
}

//Declare our foodo() function that takes a reference as an argument.

void foodo(double& whatever)
{
cout << whatever;    //so whatever we pass to our function (it has to be a double) it
                     //        will automatically be a reference because our function
                     //        definition says so.

return;
}

So to make a reference for int x, start with the type of variable, and add an amperstand to it, like this: int&. then add the name you want to give your reference, say voodoo, then use the = sign to assign voodoo to a variable: int& voodo = x; Now voodoo is a reference to x, and whatever we do to voodoo we do to x. HOWEVER, if we want to pass a reference to x to a function, then we don't even have to bother to make a reference to x first. All we have to do is declare in our function definition that the parameter to be passed is a reference, like so:

float x = 0;
foodo(x);               //Here you can't tell if x is passed by value or by reference,
                        //        you just have to look at the function definition to
                        //                                                   find out.

//function definition.
void foodo(float& count)
{
 cout << count;         //whatever is passed to foodo (in this case x) then "count"
                        //receives it (or rather, count becomes a reference to it).
 return;
 }

Now all you need to do in your program is have just one function, a print function. It will accept one reference as a parameter (which will be either "fahrenheit" or "celsius") and then will print it out on screen. You could have case1 in your swtich statement just have straight code to calulate the Fahrenheit and then pass that to your print function which takes a reference, and the same for case2. Or you could keep the two functions ftoc and ctof and have them return the fahrenheit" or "celsius" values (this is the better idea). Something like this:

case 1:
celsius = ftoc();
print(celsius);

case 2:
fahrenheit = ctof();
print(fahrenheit);

There .... whew, thank God I don't have a life!

commented: heroic, herculean helpfulness... nice to see +5

You sir are fricken awesome. That helped a lot.

Really good, MandrewP.

Pity that the way this site is moderated, that post will get buried, never to see the light of day again.

The code which you have written is a simple program because you haven't pass any reference at the time of calling a function.
reference means the address of a variable written as " &variable_name "

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.