# Thread: Void Functions

1. ## Void Functions

I really think I'm close to really having a grasp on this function stuff. Here's another program I'm writing. Some pointers would be greatly appreciated.

Code:
```#include <iostream>
#include <iomanip>

using namespace std;

void getData(double, double);
void computePay(double, double);
void displayNet(double);

int main()
{
double hours = 0;
double rate = 0;
double netPay = 0;

cout << fixed << showpoint << setprecision(2);

getData(rate, hours);
computePay(rate, hours);
displayNet(netPay);

return 0;
}

void getData(double& rate, double& hours)
{
cout << "Enter rate of pay: ";
cin >> rate;
cout << endl;
cout << "Enter hours: ";
cin >> hours;
cout << endl;
}

void computePay(double rate, double hours)
{
double netPay = rate * hours;
cout << endl;
}

void displayNet(double netPay)
{
cout << "Net pay: \$" << netPay << endl;
}```

2. Let's focus on the computePay function. It computes pay based on rate and hours, correct? What does it do with the result? Normally a function that computes a value returns that value. So your computePay function should probably not return void (which means it returns nothing) it should probably return the value it computed.

That's the first thing. The next thing is to figure out how to get the value returned from computePay and get it to displayNet.

Ohh, and one minor detail. Don't forget that your prototypes must match the function definition. Look at the prototype and definition of getData. They don't match.

3. Thanks Daved. Everything seems to be working fine now. Take a look... I'm gettin kinda proud that it didn't take me hours and hours to figure out this program! hehe

Code:
```#include <iostream>
#include <iomanip>

using namespace std;

void getData(double&, double&);
double computePay(double&, double&);
void displayNet(double, double);

int main()
{
double hours = 0;
double rate = 0;
double netPay = 0;

cout << fixed << showpoint << setprecision(2);

getData(rate, hours);
computePay(rate, hours);
displayNet(rate, hours);

return 0;
}

void getData(double& rate, double& hours)
{
cout << "Enter rate of pay: ";
cin >> rate;
cout << endl;
cout << "Enter hours: ";
cin >> hours;
cout << endl;
}

double computePay(double& rate, double& hours)
{
double netPay = rate * hours;
return netPay;
}

void displayNet(double rate, double hours)
{
cout << "Net pay: \$" << computePay(rate, hours) << endl;
}```

4. Well done.

Now that it is working, here are a few more things to look at. First, remember (or learn) that a reference variable is used when you want to update the variable in the calling function. For example, let's say you have a variable called rate that you initialized to 0 in your main function. And then you called a function that is supposed to ask the user for a rate and update that variable for you. The function should take a reference as a parameter because it will change the value of the rate variable passed to it. In your case, that is what you did, you made getData take reference parameters (indicated by the & sign). This was correct because getData changes the values of the variables passed to it (the rate and hours variables from inside of main).

Now, on the other hand, the computePay function does not change its parameters. It only uses the values for other purposes. So taking references (indicated by the & sign) is unnecessary. This doesn't break your code, but it is unnecessary and might cause confusion later.

The second thing to look at is the fact that you call computePay twice. That is not necessary. Your displayNet function can just take a single value as a parameter (the netPay already computed) and display it. You have to figure out how to use the return value of the computePay function that you call in main and get it to the displayNet function without calling computePay again.

Finally, you should always remember that variables declared in one scope are different from those declared in another scope even if they have the same name. Try changing the name of each variable you declare by adding the name of the function it is declared in to it. For example, in main, instead of rate, hours, and netPay, change the names to main_rate, main_hours, and main_netPay. Do that for each function (including the function parameters) and then follow the logic of the code to see how values are passed around and updated.

BTW, I suggest playing around with these things on a separate copy of the file so you don't lose the working version. Sometimes you play with things so much that you can't get it back working again.

5. Alrighty.... been workin on the program. I'm still getting one compiling error... "redefinition of formal parameter 'netPay'". Here's the code. Help?!?

Code:
```#include <iostream>
#include <iomanip>

using namespace std;

void getData(double&, double&);
void computePay(double&, double&, double&);
void displayNet(double&);

int main()
{
double hours = 0;
double rate = 0;
double netPay = 0;

cout << fixed << showpoint << setprecision(2);

getData(rate, hours);
computePay(rate, hours, netPay);
displayNet(netPay);

return 0;
}

void getData(double& rate, double& hours)
{
cout << "Enter rate of pay: ";
cin >> rate;
cout << endl;
cout << "Enter hours: ";
cin >> hours;
cout << endl;
}

void computePay(double& rate, double& hours, double& netPay)
{
double netPay = rate * hours;
}

void displayNet(double& netPay)
{
cout << "Net pay: \$" << netPay << endl;
}```

6. The topic of this thread is void functions. Were you supposed to complete this assignment with only using void functions? If so, then you can disregard most of what I'm about to say.

The generic use of a function is to give it some data and let it calculate some result. The computePay function is typical in that regard. You give it some data, and it calculates something from that data, then returns that value.

Given that, I liked it better when your computePay function returned the value it computed. You changed it back to return void. Why? I thought that your computePay function was almost perfect the last time.

The thing you didn't seem to understand is that the reference parameters (indicated by the & sign) are only necessary in the getData function. You added more of them instead of removing them like I was suggesting. Again, this won't break your function right now, but you should understand the difference and be able to identify when you should pass by reference and when you should pass by value.

So my advice is to try to understand when to use a reference and when not to. Ask if you aren't sure of anything. Give your understanding in English (not code) so we can see if you understand correctly. After you do that we can apply it to your code.

Also try to understand the concept of a function that returns a value. Again, if you are supposed to use functions that return void because of your assignment, you should say so, otherwise you should understand how to get the return value of a function. For example, if you switch back to your previous version of computePay that returned a double, you need to find out how to store that double in a variable to be used again. Again, ask questions if you aren't sure after thinking about it.

If you are supposed to be using only void functions, then never mind all that. Your problem is that you redefine netPay, just like the error says. Look at the line of code pointed to by the compiler error and see if you can see how you are defining a variable instead of just using it.

7. void displayNet(double& netPay)
{
cout << "Net pay: \$" << netPay << endl;
}
This function doesn't modify 'netPay', and so there is no need for you to use a reference. As a good rule of thumb, if you aren't changing the varaibles that are passed to a function, they should be defined as constant.
Code:
```void displayNet( const double netPay )
{
cout << "Net pay: \$" << netPay << endl;
}```
With member functions it's often a good idea to make the whole function constant. It just lets people know that what they're passing isn't going to change. It helps since references are ugly little things and you can't really tell if you're passing a function a reference or copying by value without going and looking up the function call. Which is fine I suppose if you're talking about a few functions total, but it gets messy with a ton of things bouncing around.

That's why I like pointers better than references. Your compiler will let you know if you're doing it wrong.

Quzah.

8. Yes, we are only supposed to be using void functions for this. The thing that I don't get is that we haven't learned anything about a void function returning a value.... so I'm still a little confused on how it should work right in the first place. Here are some more revisions. I can execute the program now, but it's coming out "\$0.00" for the net pay.

Code:
```#include <iostream>
#include <iomanip>

using namespace std;

void getData(double&, double&);
void computePay(double, double, double);
void displayNet(double);

int main()
{
double hours = 0;
double rate = 0;
double netPay = 0;

cout << fixed << showpoint << setprecision(2);

getData(rate, hours);
computePay(rate, hours, netPay);
displayNet(netPay);

return 0;
}

void getData(double& rate, double& hours)
{
cout << "Enter rate of pay: ";
cin >> rate;
cout << endl;
cout << "Enter hours: ";
cin >> hours;
cout << endl;
}

void computePay(double rate, double hours, double netPay)
{
netPay = rate * hours;
}

void displayNet(double netPay)
{
cout << "Net pay: \$" << netPay << endl;
}```

9. well, you're right... a void function doesn't return anything. but you can pass in a reference (like has been mentioned), and whatever changes you make to that variable in the function are reflected in the calling block of code. for example, taking (and fixing) your computePay function, it can be done two ways:

with a reference:
Code:
```void computePay(double rate, double hours, double &netPay)
{
netPay = rate * hours;
}```
or with a return type (non-void function):
Code:
```double computePay(double rate, double hours)
{
return = rate * hours;
}```
I prefer to avoid references, because it's just easier to keep track of things like that. usually if I need a function to change a whole bunch of data, I'll use a class and create a method.

10. Ok, well that's good to know that you are only supposed to use "void functions". A void function is just a function that doesn't return anything. Normally, you would return the answer you find, but you are doing an exercise to learn about passing by reference, so you are doing it this other way.

major_small's first version is what you want (basically add the & to the netPay parameter of the computePay function), don't forget to add it to the prototype also.

You still seem to not understand when to use a reference and when not to. Since that seems to be the whole point of the assignment, I'd go back and try to understand the concepts again.

11. <derail>
Originally Posted by MyntiFresh
Some pointers would be greatly appreciated.
Code:
```void *pVoid;
int *pInt;
double *pDouble;```
Need any more?
</derail>

Popular pages Recent additions