Problems With Void Functions

closed account (iT7X92yv)
I'm having an issue with my latest homework problem. Here's the problem as described in the book:

"Write the C++ code for a void function that receives four int variables: the first two by value and the last two by reference. Name the formal parameters n1, n2, sum, and diff. The function should calculate the sum of the two variables passed by value and then store the result in the first variable passed by reference. It also should calculate the difference between the two variables passed by value and then store the result in the second variable passed by reference. When calculating the difference, subtract the contents of the n2 variable from the contents of the n1 variable. Name the function calcSumAndDiff. Also write an appropriate function prototype for the calcSumAndDiff function. In addition, write a statement that invokes the calcSumAndDiff function, passing it the num1, num2, numSum, and numDiff variables."

Here's my code thus far:

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
#include <iostream>
#include <iomanip>
using namespace std;

//function prototype
void calcSumAndDiff(int n1, int n2, int sum, int diff);

int main()
{
	//declare variables
	int num1 = 0;
	int num2 = 0;
	int numSum = 0;
	int numDiff = 0;

	//get input items

	cout << "First number: ";
	cin >> num1;
	cout << "Second number: ";
	cin >> num2;

	//Adding and subtracting the numbers
	calcSumAndDiff(num1, num2, numSum, numDiff);
	
	//display the sum and difference
	cout << "Sum: " << numSum << endl;
	cout << "Difference: " << numDiff << endl;

	system("pause");
	return 0;
} //end of main function

//*****function definitions*****
void calcSumAndDiff(int n1, int n2, int sum, int diff)
{
	sum = n1 + n2;
	diff = n1 - n2;
} //end of calcSumAndDiff function 



The issue I'm having is that no matter what numbers I put in, it displays the sum and difference as 0. I more than likely misinterpreted something while reading the book, but I'm not sure what it is. Any help with solving my problem would be greatly appreciated.
Modify list of parameters to
void calcSumAndDiff(int n1, int n2, int& sum, int& diff)

& stands for reference.
Topic archived. No new replies allowed.