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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// This program takes two numbers (payRate & hours)
// and multiplies them to get grosspay.
// It then calculates net pay by subtracting 15%

#include <iostream>
#include <iomanip>
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.