I made a program that calculates the equation ( gives me the values x1 and x2 ). But the problem is though , i needed to write 2 seperate functions for x1 and x2 , even though i only needed to change a “+” sign to a “-” sign to get x2. Is it possible to get the same output put only using one function ? Heres the code :
double equation(double a, double b, double c) {
double argument, x1;
argument = sqrt(pow(b, 2) - 4*a*c);
x1 = ( -b + argument ) / (2 * a);
return x1;
}
double equation2(double a, double b, double c) {
double argument, x2;
argument = sqrt(pow(b, 2) - 4*a*c);
x2 = ( -b - argument ) / (2 * a); // here i changed the "+" sign to "-"
return x2;
}
Thank you in advance !
Theres a couple different ways you can do this. Gareth mentions one, but another is to use output parameters.
Using pointers as input parameters, you can populate them both in one function, and you don’t need to return anything
Then call it from your main code: