How do to call this function

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cmath>

void quadsolve(double a, double b, double c, bool& real, double& x1, double& x2){
    double delta = b*b - 4*a*c;
    if(delta>0){
        double sqdelta = std::sqrt(delta);
        x1 = ((-b+sqdelta)/2*a);
        x2 = ((-b-sqdelta)/2*a);
        real=true;
    }
    else{
        real=false;
    }
}

void quadsolve(double a, double b, double c, bool& real, double& x1, double& x2);

int main(){
    double a,b,c;
    std::cout << " please enter a b c" << std::endl;
    std::cin >> a >> b >> c;
    double s1, s2;
    bool fake;

    if(quadsolve(a, b, c, fake, s1, s2)){
        std::cout << " the roots of the equation are " << s1 << " and " << s2 << std::endl;
    }
    else{
          std::cout << " the equation has no real roots" << std::endl;
    }
    return 0;
}



I am trying to learn how to work with void and hence not using quadsolve as a bool but instead declaring a variable (real) as a bool and passing on true/false through this through the method of passing by reference.

now my issue is that how could i call on the function quadsolve such that i could use this bool to then use it in a condition to output whether it is real and its possible real roots if they exist.

I would appreciate any help.
Last edited on
You pass the parameter bool& real to the function.

The function sets it to true or false.

When the function has returned, you look at the parameter to see whether it has been set to true or false.
Yes, what i am confused with is how do i call the function in the main?
quadsolve(a, b, c, fake, s1, s2);
AH! I did not know you can just call a function like this, sorry still a beginner. Thank you.
Topic archived. No new replies allowed.