I want to return two float variables from a bool function althought i dont know how to do it. What should i write in main? Here is my code.
bool triwnymo(int a, int b, int c, float& x1, float& x2){
int d;
d=diak(a,b,c);
if(d>0){
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
return x1,x2;
return true;
}else if(d==0){
x1=-b/(2*a);
x2=x1;
return x1,x2;
return true;
}else{
return false;
}
}
You can use a custom data structure to return as many values you like:
struct triwnymo_result_type {
float root1 = 0.0;
float root2 = 0.0;
bool has_solution = false;
};
triwnymo_result_type triwnymo(int a, int b, int c){
int d = diak(a,b,c);
if(d>0) {
return {(-b+sqrt(d))/(2*a), (-b-sqrt(d))/(2*a), true};
} else if(d==0) {
int x=-b/(2*a);
return {x,x,true};
} else {
return {0.0,0.0,false};
}
}
Note that return x1,x2;
is using the comma operator. The comma operator evaluates both operands, discards the first and results in the value of the latter. Thats not what you want. Also return true;
after you already returned from the function is never reached. You can only return from a function once.
I didn't want to change too much, but you don't really need to distinguish between d>0
and d==0
because when d==0
then -b+sqrt(d) == -b-sqrt(d) == -b
.