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);
double radius(double distance3);

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 << "Radius: " << radius(distance3);
	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;
}

double radius(double distance3)
{
	double rad;

	rad = distance3 / 2;

	return rad;
}

Recommended Answers

All 3 Replies

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);
}

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?

double radius(double and distance3)

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()
{
            your other variables
            double dist, rad;

            further down....
 
            dist = distance(center1,center2, etc);
            rad = radius(center3);
             
}

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; .

What restrictment says is technically correct, but since you have the return values (and only need 1 per function) you can go ahead and get them in the traditional way. If you wanted to, you could create a reference variable dist and send it in with your arguments, but just stick with what you have for now.

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.