You are using a version without Ads of this website. Please, consider donating:

### Problems With Void Functions

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:

 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````#include #include 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.