Answer:
Here is the function CoordTransform() that transforms the function's first two input parameters xVal and yVal into two output parameters xValNew and yValNew
void CoordTransform (int xVal ,int yVal ,int& xValNew, int& yValNew) { //function definition
xValNew = (xVal +1) *2; // adds 1 to old value (xVal) , multiplies the result by 2 and assigns that new value to xValNew
yValNew = (yVal +1) *2; } // adds 1 to old value (yVal) , multiplies the result by 2 and assigns that new value to yValNew
Explanation:
Here is the complete program:
#include <iostream> //to use input output functions
using namespace std; // to access objects like cin cout
void CoordTransform (int xVal ,int yVal ,int& xValNew, int& yValNew) { //function that takes four arguments
xValNew = (xVal +1) *2; // computes new value and assigns it to xValNew
yValNew = (yVal +1) *2;} // computes new value and assigns it to yValNew
int main() { // start of main function
//declares 4 int type variables xValNew, yValNew, xValUser, yValUser
int xValNew;
int yValNew;
int xValUser;
int yValUser;
cin >> xValUser; // takes value of xValUser from user
cin >> yValUser; // takes value of yValUser from user
CoordTransform(xValUser, yValUser, xValNew, yValNew); //function call
cout << "(" << xValUser << ", " << yValUser << ") becomes (" << xValNew << ", " << yValNew << ")" << endl; //prints the old and new values
return 0;}
In this program the function CoordTransform receives a reference to a variables xValNew and yValNew so that it can modify the values of these variables. So xValNew and yValNew variables are declared as reference so they become an alternative name for the variables xValNew and yValNew used in main() method . These variables are declared as reference in the function CoordTransform() by placing ‘&’ symbol in the declaration.
Suppose user enters 3 as value of xValUser and 4 as value of yValUser
Now when the function CoordTransform() is called it works as follows:
xValNew = (xVal +1) *2; this statement becomes:
xValNew = (3 + 1) * 2
xValNew = 4 * 2
xValNew = 8
Now this value is assigned to the xValNew of calling function (main) xValNew variable.
yValNew = (yVal +1) *2;
this statement becomes:
yValNew = (4 + 1) * 2
yValNew = 5 * 2
yValNew = 10
Now this value is assigned to the yValNew of calling function (main) yValNew variable.
So the output of the above program is:
(3, 4) becomes (8, 10)