Hey guys, pretty confused about what a "pass-by-reference" function is and does. No one will put it into simple English for me and Google has become my worst enemy on the topic.

What the Program has to do is convert the five-numbers of a zip code (for example: "64110") into words ("Siz Four One One 0"). It has to display the First and last name of the resident, their street, city, state, and then the zipcode. HERE IS THE KICKER: It must all be read from an input file! Below is my coding so far. It will not compile in Visual Studio (2008), and as you can tell from it being cut-off, I don't know what to do:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

bool checkFileInput (ifstream &fin);
bool checkFileOutput (ofstream &fout);
int changeZipcode (string firstName, string lastName, int houseNumber, int street, int city, int state, int zipcode, int zipChange, string fullzipcode);


int main()
{
    ifstream fin("program5.txt");
    ofstream fout("output5.txt");

    
    fin.open("program5.txt");
    fout.open("output5.txt");

if(! checkFileInput(fin))
{
    exit(2);
}
if(! checkFileOutput(fout))
    {
        exit(3);
    }

    cout << "Go check the output file\n" << endl;
    fin.close();
    fout.close();

}
int changeZipcode (int zipcode, int fullzipcode)
{
    return 0;
}
bool checkFileInput (ifstream &fin)
{
    if(fin.good())
    {
        return true;
    }
    else
    {
        cerr << "Unable to Open file\n";
        return false;
    }
}
bool checkFileOutput (ofstream &fout)
{
    if(fout.good())
    {
        return true;
    }
    else
    {
        cerr << "Unable to write to Output file\n";
        return false;
    }
}
int changeZipcode (string firstName, string lastName, int houseNumber, int street, int city, int state, int zipcode, int zipChange string fullzipcode)
{
    while (fin >> firstName >> lastName >> houseNumber >> street >> city >> state >> zipcode)
    {
        if(zipcode>=10000)
        {
            zipChange=(zipcode/10000)
        }
        if(zipChange=1)
        {
            zipChange = one;
        }
        if(zipChange = 2)
        {
            zipChange = two;
        }
        if(zipChange = 3)
        {
            zipChange = 3
        }
        if(zipChange=4)
        {
            zipChange = four;
        }
        if(zipChange=four)
        {
            zipChange = four;
        }
        if(zipChange=5)
        {
            zipChange = five;
        }

}

As you can see, the number one problem I am having is using a "pass-by-reference" function to change the zipcode. I don't even know what it does or how to use it! But I have to use it. Because I thought that at least I'd be getting somewhere, I went ahead and put the information into the int function "changeZipcode". But, now I'm having trouble figuring out how to get the program to read each individual number and then convert it into words! Can anyone help me?

In C++, you can declare a function, constructor, or operator to accept a type by reference and not value.

For example, the declaration of the function--

void foo(int);

--states that the function foo takes [a copy of] an integer and returns void.

So the following would be legal--

int main(){
   foo(3); // legal
   int x = 4;
   foo(x); // legal
   return 0;
}

--however, you could instead declare a foo to take a reference to an int instead--

void foo(int&);

-- which means foo takes [a reference of] an int and returns void. Before proceeding, let's think of what a reference is.

Reference(programming) - the underlying address of an object, or type.


Hmmm... what does that mean?

Ok, in English... let's say you live at a particular address. Your house sits on top of that address, so therefore in order to access your house I need only to know your address.

If, for example, I sent someone information that contained your address, that individual would know where the house is and be able to access it (well in reality that's a big maybe, but in programming that's true to a certain extent).

int main(){
 //  foo(3); // illegal, because '3' itself is not an address!
 int x = 4;
 foo(x); // legal, because the value x has an address and therefore can be passed by reference
 return 0;
}

The real question is, what is the benefit of this?

Well depending on your machine (or more importantly, what operating system you're running on), you can save a tremendous amount of time passing information by reference instead of by value because when you pass by value you're passing only a copy of a particular type (and not the address itself). The reason passing 3 in the former foo that took int by value was legal was because 3 by itself doesn't have an address. In order for something to have an address, it must exist somewhere on the stack, heap, or another location in memory.

If you pass a big object by value for example, the object will be copied and the copy will be used as part of the instruction set that exists within the method. Copying takes time, which means constantly using a method that took an object by value could cause a serious performance hit for your program.

A way to solve this problem is to pass by reference. Technically you're still passing a copy of a value to the method (in this case, a copy of the reference), but it's significantly smaller than a copy of an entire object. The reason for this is that no matter how big an object gets, its address is always the same size! This is amazing, but it makes sense.

I could continue on and explain why an address is always the same size, but to save time and to stick to the original point of clearing some mist I'll spare you XP.

Think of it this way - passing a copy of the address to the method means that the method knows where that object is and can perform operations on that object with the given reference.

Maybe a program will be a better explanation?

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

void foo(int&);

int main(){

    int x = 4; // declaring and defining x to be of type int with value 4

   // x is a reference-variable and therefore has an address

    foo(x); // passing the address of x into the function

    cout << x << endl; // printing the result
    cin.ignore(INT_MAX, '\n');
    cin.get();
    return 0;
}

// method accepts a reference to an int
void foo(int& ref){

    ref += 5; // increments the value located on top of the address by 5

}

The result should be 9.

Notice that if you change foo to accept values (copy of the type) instead of references (copy of the reference of the type) then instead of 9 you will get 4, because you would be passing a copy of the value that exists on the address of x (i.e. a 'copy' of your house) instead of the type that existed above the address of x itself.

Also another reason to pass by reference is to return multiple values from one function, instead of one (the function doesn't have to return void, nor does it only have to have one reference parameter, but it was done in this particular case for simplicity).

Hi
Simplified in 1 sentence:

When an argument of a method is specified as a reference to a variable and this method is called, THIS METHOD IS ABLE TO CHANGE THE VALUE OF THAT VARIABLE IN CONTRAST TO PASSING THE ARGUMENT BY VALUE WHERE IT DOES NOT CHANGE THE VALUE OF THE VARIABLE PASSED.

ok ok ok I agree a rather long.... sentense :D

P.S. Later when u start programming using classes u will find out that there is one more reason why references are important. They are valuable when declaring friend classes and copying fast objects of the same class.

Gracias mi amigos! Finally understood what a "pass-by-reference" function is. Seems all to simple now, didn't last night.

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.