(ii) Write a void function that receives four int parameters: 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 parameters passed by value and then store the result in the first variable passed by reference. It should calculate the difference between the two parameters passed by value and then store the result in the second parameter passed by reference. When calculating the difference, subtract the larger value from the smaller value. Name the function calcSumAndDiff.
void calcSumAndDiff(int n1, int n2, int &sum, int &diff)
{
sum = n1 + n2;
if(n1 < n2)
diff = n2 - n1;
else
diff = n1 - n2;
}
Comments
Leave a comment