how to return multiple values

how do I return both of these values back into my program?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 double getInput (double hoursworked, double payrate )

{



	
	
	
	//  request the total hours worked
	std::cout<< "\n Please enter the total hours worked  \n"; 

	// input the total hours worked
	std::cin>> hoursworked;

	//request employee's earning wage information
	std::cout<<"\nEnter the pay rate\n";

	// input payrate for requested employee
	std::cin>>payrate;

  return ??;
Last edited on
Pass by reference, don't return anything.
Last edited on
As lb mentioned instead of passing by copies pass by reference you would do this like this

1
2
3
4
void getInput( double &hoursworked , double &payrate )
{
   //same body you have will work
}


It will directly modify the variables being passed through the function instead of creating a copy of it then modifying that one.

That being said an example would be this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

void ex( int &a , int b )
{
    a = 2;
    b = 2;
    std::cout << "During: a = " << a << " | b = " << b << std::endl;
}

int main()
{
    int a = 1 , b = 1;
    std::cout << "Before: a = " << a << " | b = " << b << std::endl;
    ex( a , b );
    std::cout << "After: a = " << a << " | b = " << b << std::endl;
}


As you can see b is only modified inside of the local function of ex where as a is being modified inside of the local function and also modified inside of the main function since it is modifying the value of a at the reference( memory address of a ).

http://www.cplusplus.com/doc/tutorial/functions2/
http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/
https://www.google.com/#q=how+to+pass+by+reference+in+c%2B%2B
Topic archived. No new replies allowed.