### function needs to return two values, havent used structures/pointers before

Hello, I've got a problem in which I have a program where I am to find the gross and net pay of an employee when they give their hours worked and pay rate. My only problem is that for this lab, the function computePaycheck has to return 2 variables, which we haven't gone over yet. I've looked into structures and pointers but I'm not really sure how to go about modifying because I don't totally understand the two.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556`` ``````// This program takes two numbers (payRate & hours) // and multiplies them to get grosspay. // It then calculates net pay by subtracting 15% #include #include using namespace std; //Function prototypes void printDescription(); void computePaycheck(float, int, float &, float &); int main() { float payRate; float grossPay; float netPay; int hours; cout << setprecision(2) << fixed; cout << "Welcome to the Pay Roll Program" << endl; printDescription(); cout << "Please input the pay per hour" << endl; cin >> payRate; cout << endl << "Please input the number of hours worked" << endl; cin >> hours; cout << endl << endl; //Call to computePaycheck function // Fill in the code to output grossPay and netPay return 0; } void printDescription() // The function heading { cout << "************************************************" << endl << endl; cout << "This program takes two numbers (payRate & hours)" << endl; cout << "and multiplies them to get gross pay " << endl; cout << "it then calculates net pay by subtracting 15%" << endl; cout << "************************************************" << endl << endl; } // ********************************************************************* // computePaycheck // // task: This function takes payRate and hours and multiples them to // get gross pay and then finds net pay by subtracting 15%. // data in: pay rate and time in hours worked // data out: the gross and net pay // // ******************************************************************** void computePaycheck(float rate, int time, float &gross, float &net) { gross = rate * time; net = gross * .15; }``````
computePaycheck doesn't actually return anything, but it modifies the variables that the caller passes ('gross' and 'net' are both passed by reference).

So you would call it like:
`computePaycheck(payRate, hours, grossPay, netPay);`

grossPay and netPay will be modified from within the function, and their values will be updateed in main().
Last edited on
So basically my reference variables can be called back to main and then outputted...? No need for pointers or structures?
Yes. Reference parameters are effectively making an alias -- the parameter you call the function with IS the same variable inside the function -- its name changed, but nothing else.

reference parameters are also used to avoid copying large items, in which case they are often constant.

 ``1234567891011121314`` ``````to do it with a struct is not bad either, though. struct twothings { double d; int i; }; twothings foo() { twothings tt; tt.d = 3.14; tt.i = 42; return tt; }``````

c++ has a couple of built in things so you don't even need the struct .. you can read this later:
https://en.cppreference.com/w/cpp/utility/tuple

there are, like many things, a variety of ways to do this. I like the tuple and dislike reference parameters, but that is just style, they all work and no reason for one over another most of the time any approach will do.
Last edited on
Topic archived. No new replies allowed.