We're supposed to write a function that calculates both the x and y coordinates of the upper left corner of the square that just contains a circle, given the x and y coordinates of the center of the circle, and the radius of the circle. Return the x and y coordinates of the upper left corner of the square to the calling function.
I have the function write, but I don't know how to just return the value from the function without outputting anything. Please help. Here's my code:

  1. int circle_x,circle_y,circle_radius,square_x,square_y;
  2. int x_y_square_coordinates (int circle_x, int circle_y, int circle_radius, int& square_x, int& square_y)
  3. {
  4. square_x = circle_x-circle_radius;
  5. square_y = circle_y-circle_radius;
  6. return square_x, square_y;
  7. }


  8. int main()
  9. {
  10. cout << "Enter the x and y coordinates of the center of the circle and the radius of the circle." << endl;
  11. cout << "x:";
  12. cin >> circle_x;
  13. cout << "y:";
  14. cin >> circle_y;
  15. cout << "radius:";
  16. cin >> circle_radius;
  17. cout << x_y_square_coordinates (circle_x,circle_y,circle_radius,square_x,square_y);

  18. cin.get();
  19. getchar();

  20. return 0;
  21. }

Code tags. And don't add the numbers. The code tags will do that:

[code=cplusplus] // paste your code here

[/code]

int circle_x, circle_y, circle_radius, square_x, square_y;


int x_y_square_coordinates(int circle_x, int circle_y, int circle_radius, int& square_x, int& square_y) 
{
    square_x = circle_x-circle_radius;
    square_y = circle_y-circle_radius;
    return square_x, square_y;
}


int main() 
{
    cout << "Enter the x and y coordinates of the center of the circle and the radius of the circle." << endl;
    cout << "x:";
    cin >> circle_x;
    cout << "y:";
    cin >> circle_y;
    cout << "radius:";
    cin >> circle_radius;
    cout << x_y_square_coordinates(circle_x, circle_y, circle_radius, square_x, square_y);    
    cin.get();
    getchar();    
    return 0;
}

You can't return two integers from a function that returns one integer. You either need to create a struct or array that contains both integers you want to return and have the function return that, or pass one or both integers by reference, then change them inside the function (looks like you've already done that in your function, though passing a global variable by reference is redundant).

In YOUR case, you don't have to do either, since you made both square_x and square_y global. You don't need to return anything from your function. You can just make it a void function. You still have the confusion of passing a global variable by reference, which is legal, but generally not done. You probably don't need all of these variables and can delete some of them, or at least not pass them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.