Hello all,
I want to rewrite the quadratic equation as a function and as a function in the main. Below are the two programs but was not giving accourate answer.

For the function alone:
Code:
```float quadratic(float x1, float x2)
{
float a, b, c, d, g, h=0.0;
printf ("\nEnter value for a\n");
scanf ("%f", &a);
printf ("\nEnter value for b\n");
scanf ("%f", &b);
printf ("\nEnter value for c\n");
scanf ("%f", &c);
d=sqrt((b*b)-4*a*c);
g=2*a;
if (g==h)
{
printf ("\nYou have performed an illegal operation as the divisor cannot be zero.");
}
else
{
x1=(-b+d/g);
x2=(-b-d/g);
printf ("\nFirst real root is," x1);
printf ("\nSecond real root is," x2);
}
return (x1, x2);
}```
it was not printing any values neither showing any calculations done

The second one that has main with it
Code:
```/* program to calculate the root of a quadratic equation*/
#include <stdio.h>
#include <math.h>

float quadratic(float a, float b, float c)
{
float d, g, h=0.0, x1;
g=2*a;
if (g==h)
{
printf ("\nYou have performed an illegal operation as the divisor cannot be zero.");
}
else
{
x1=(-b+((b*b)-4*a*c))/g;
}
return (x1);
}

float quadratic1(float a, float b, float c)
{
float d, g, h=0.0, x2;
g=2*a;
if (g==h)
{
printf ("\nYou have performed an illegal operation as the divisor cannot be zero.");
}
else
{
x2=(-b-((b*b)-4*a*c))/g;
}
return (x2);
}
main()
{
float a, b, c, x1, x2;
printf ("\nEnter value for a\n");
scanf ("%f", &a);
printf ("\nEnter value for b\n");
scanf ("%f", &b);
printf ("\nEnter value for c\n");
scanf ("%f", &c);
printf("\na=%f,", a);
printf("\nb=%f,", b);
printf("\nc=%f,", c);
printf("\nThe real roots are %f, %f", x1, x2);
}```

2. Code:
`  return (x1, x2);`
Don't return 2 values. I would suggest returning 0 as well.

You're program ends when main ends, closing the window before you can see anything. you can use getch() to pause it until a key is pressed.

3. Originally Posted by skorman00
Code:
`  return (x1, x2);`
Don't return 2 values. I would suggest returning 0 as well.

You're program ends when main ends, closing the window before you can see anything. you can use getch() to pause it until a key is pressed.
It doesnt have a main function, it is just a user-defined function, also i want it to print out the result but was not doing that.

[QUOTE=abs.emailverify]
Code:
```float quadratic(float x1, float x2)
{
float a, b, c, d, g, h=0.0;
printf ("\nEnter value for a\n");
scanf ("%f", &a);
printf ("\nEnter value for b\n");
scanf ("%f", &b);
printf ("\nEnter value for c\n");
scanf ("%f", &c);
d=sqrt((b*b)-4*a*c);
g=2*a;
if (g==h)
{
printf ("\nYou have performed an illegal operation as the divisor cannot be zero.");
}
else
{
x1=(-b+d/g);
x2=(-b-d/g);
printf ("\nFirst real root is," x1);
printf ("\nSecond real root is," x2);
}
return (x1, x2);
}```
it was not printing any values neither showing any calculations done

4. Here's the 2 things that are wrong :

1) A function in C can't return a tuple. You'd have to return a float pointer if you actually want to return both those answers.

2) There's a reason you aren't seeing any calculation being done, namely that

Code:
`printf ("\nFirst real root is," x1);`
won't print what you expect it to, while

Code:
`printf ("\nFirst real root is, %f" x1);`
probably will.

5. 1) A function in C can't return a tuple. You'd have to return a float pointer if you actually want to return both those answers.
Or you could return a structure containing several variables.
Code:
```struct twofloats_t {
float a, b;
};```
Code:
`printf ("\nFirst real root is," x1);`
won't print what you expect it to, while
Code:
`printf ("\nFirst real root is, %f" x1);`
probably will.
It probably won't, unless you add a comma too.
Code:
`printf ("\nFirst real root is, %f", x1);`

6. If a = 0, the the 1 root is given by -c/b.