Void Functions

Edit: Thanks for the pointers, I've solved my four problems with the help and some assistance from a classmate that took the class already so knows it a bit better. Thanks =)

Hello all,

I have a decent beginner's background in programming. I have taken and passed classes with complete understanding of concepts covered in Visual Basic as well as Java. I am currently in C++ nearing the end of the book. It took me a little bit but I pretty much wrapped my head around Value-Returning Functions. Void Functions have left me completely lost though. If there's any advice one may be able to give, I'd appreciate it. I know that once I understand one program, I'll have no problem figuring out the rest.

I'm running Visual C++ 2005, if that matters.

Here is my objective: To create a simple payroll program using a main() function and four void functions named getInput(), calcFedTaxes(), calcNetPay(), and displayInfo(). The FWT rate is 20% of the weekly salary, and the FICA rate is 8% of the weekly salary.

getInput() only gets the name and weekly salary

calcFedTaxes() calculates the FWT with FWT = weekly salary * FWT rate and FICA with FICA = weekly salary * FICA rate.

calcNetPay() calculates net pay by weekly net pay = weekly salary - FWT - FICA.

displayInfo() displays the name, FWT, FICA, and weekly net pay.

Here is my (extremely unfinished) code, I just can't seem to grasp where to go next (if what I have is even right so far).

Code:

#include <iostream>
#include <string>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::setprecision;
using std::fixed;

//function prototypes
//note: not sure where I do and don't need the ampersands
void getInput(string &, double &);
void calcFedTaxes(double &, double &);
void calcNetPay(double &, double &, double &);
void displayInfo(string &, double &, double &, double &);

int main()
{
//declare constants and variables
const double FWT_RATE = 0.2;
const double FICA_RATE = 0.08;
string name = "";
double salary = 0.0;
double fwtTax = 0.0;
double ficaTax = 0.0;
double netPay = 0.0;

//enter input items

//calculate taxes

//calculate net pay

//display name, gross, taxes, and net
cout << fixed << setprecision(2);

return 0;
} //end of main function

//*****function definitions*****




As you can see, I haven't made much progress beyond my general outline. I'm not asking for anyone to write the code for me, just anything to help point me in the right direction, or tell me if I've made any mistakes up to this point.

Much appreciated,

Derek
Last edited on
Hello Derek.
Void means that the function will not return any value by its name. Considering that, there must be another way of returning variables from a function. In C++ , arguments for functions can be passed in two ways : by value or by reference. When passing a argument by value , just the value of the parameter is copied and used in that function , so any change that you made to the variable is lost when the function ends.This means you can't use variables passed by value to modify the value of the arguments.
Passing arguments by reference means basically to pass the address of that variable to the function.This enables you to change the value of the variable,since you have access to the zone of memory where that value is kept. When the function ends, that address is still valid and so are the operations made on the value of the argument passed.
Let's say you want to add 3 numbers and return the sum. Instead of calling the function sum = function(a,b,c); you write function(a,b,c,&sum); and you accomplish the same thing.
In conclusion , to understand and use void functions you must read about passing arguments by value and by reference a bit.You will than be capable of returning values in functions with no type (or void).
Thanks, I'm going to get some rest and come back at it with new eyes. I'm going to take this post I see here as well as the post I saw in my e-mail and see if I can figure out the rest of this. I think the concept seems clearer now than the way my book attempted to explain it across 50 pages. Hopefully I'll be able to complete the four programs I was looking at. I'll return if I encounter any further problems, I'll mention them. Or if I am able to successfully create all four programs I'm aiming for, I'll return to mark this thread as resolved. Thanks much for pointing me in the right direction.
Topic archived. No new replies allowed.