Need help returning two values from a function

I'm doing the ever so popular quadratic equation and the topic I saw on here didn't use functions to solve it. I can only get the function to return one of the roots. Can someone help?

this is the two real roots part of my function
1
2
3
4
5
6
7
8
9
10
11
12
13
double quadraticEquation(double var1,double var2, double var3)
	{
	double result1 = 0;
	double result2 = 0;
	
	if (pow(var2,2)-(4*var1*var3) > 0)	
		{
		result1 = (-var2 + sqrt(pow(var2,2)-(4*var1*var3)))/(2*var1);
		result2 = (-var2 - sqrt(pow(var2,2)-(4*var1*var3)))/(2*var1);
	
		return result1;
		return result2;
		}


and then in int main I dont know how to display result1 and result 2

this is what I have..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
case 1:
			   
{
double a,b,c = 0;
double value = 0;
				
cout<<endl;
cout<<"In the form ax^2 + bx + c = 0"<<endl<<endl;
cout<<"What is the 'a' value - ";
cin>>a;
cout<<"What is the 'b' value - ";
cin>>b;
cout<<"What is the 'c' value - ";
cin>>c;

value = quadraticEquation(a,b,c);
cout<<"The Roots Are "<<?????<<" and "<<????????<<endl;
				
break;
}

Last edited on
A function can't return more than one value.
You might need to use an array to pack multiple return values into one variable.
fixed it thank you Moschops

1
2
3
4
5
6
7
8
void quadraticEquation(double var1,double var2, double var3, double& result1, double& result2)
	{
	if (pow(var2,2)-(4*var1*var3) > 0)	
		{
		result1 = (-var2 + sqrt(pow(var2,2)-(4*var1*var3)))/(2*var1);
		result2 = (-var2 - sqrt(pow(var2,2)-(4*var1*var3)))/(2*var1);
		}
	}
Topic archived. No new replies allowed.