How can I call a value that was created in 1 function into another function so that it can be used as a variable? (i.e how do I implement the values that I found in my distance function, and use them in my radius function...)

``````#include <iostream>
#include <cmath>
using namespace std;

double distance(double center1, double center2, double point1, double point2);

int main()
{
double center1, center2, point1, point2, distance3;

cout << "Please Enter X-Value of Center: ";
cin >> center1;
cout << "Please Enter Y-Value of Center: ";
cin >> center2;
cout << endl;

cout << "Please Enter X-Value of Point: ";
cin >> point1;
cout << "Please Enter Y-Value of Point: ";
cin >> point2;
cout << endl;

cout << "Distance between Points: " << distance(center1,center2,point1,point2);
cout << endl;
cout << endl;

} //End of Main

double distance(double center1, double center2, double point1, double point2)
{
double distance1, distance2, distance3;

distance1 = pow((point1 - center1), 2);
distance2 = pow((point2 - center2), 2);
distance3 = pow((distance1 + distance2), 0.5);

return distance3;
}

{

}``````
3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by jonsca

You have to send the variable back to the main function, and implement it into the second function. You can do this using the ampersand character, '&'.

IE

``````function(double & value)
{
double value = 3;
}
function2(double & value)
{
cout << value;
}
main()
{
function(value);
function2(value);
}``````

Edited by restrictment: n/a

You have to send the variable back to the main function, and implement it into the second function. You can do this using the ampersand character, '&'.

IE

``````function(double & value)
{
double value = 3;
}
function2(double & value)
{
cout << value;
}
main()
{
function(value);
function2(value);
}``````

This is going to sound bad, but can you use the terms from my code please.....I'm not sure what you mean otherwise....I'm not clear on function(double & value).....how would I code it cor the radius portion?

This is going to sound bad, but can you use the terms from my code please.....I'm not sure what you mean otherwise....I'm not clear on function(double & value).....how would I code it cor the radius portion?

In this case, since your functions are returning values (radius returns a double and distance returns a double) you can just grab those values into a local variable in main()

e.g.

``````int main()
{

further down....

dist = distance(center1,center2, etc);
Now instead of just outputting those to a cout (which was implicitly using the return value anyway) you can do whatever you want with them. e.g., `double halfdist = dist/2;` .