## How can I return value from bool function with formal parameters?

void function c++ example
value-returning function c++
function with argument and return value in c++
python function return boolean value
return void function c++
void function example
how to call a void function in c
what is a boolean function in python
```int Solve(double& root1,double& root2) //this is function in the class Eq2
{
int discrimant = b*b - 4*a*c;
if(discriminant<0)
return false;
else if(discriminant>0)
{
root1 = (-b - sqrt(discriminant))/(2*a);
root2 = (-b + sqrt(discriminant))/(2*a);
return root1;    //must return root1 and root2
}
}

int main()
{
Eq2 eq1(1, -5, 6);   //discriminant=1,root1=2,root2=3
Eq2 eq2(1, -6, 8);   //discriminant=4,root1=2,root2=4
Eq2 eq3(1, 2, 3);
Eq2 eq4(0, 0, 0);

double root1, root2;
Eq2 eqq[4] = { eq1,eq2,eq3,eq4 };
for (int i = 0; i < 4; i++)
{
eqq[i].print();
}
cout << "The roots of the equation are:"<<eq1.Solve(root1,root2);   //here
i call the function

system("pause");
return 0;
}
```

I made the function int and it is working but i can ouput just 1 root.I need to return root1 and root2.Thank you everyone for your help!I'm still in the beginning and i have a lot to learn.

Disclaimer:

While writing my own MCVE, OP edited the question. Hence, my identifiers are a little bit different. I hope this will not cause confusion...

Discriminant is such a special word, that I immediately remembered that I learnt it in math. The rest was lost but immediately refreshed from Wikipedia and what else google found.

The actual issue of OP seems to be to understand how reference parameters can be used to return values from functions without using (or additionally to) `return`. This is IMHO already covered in the other answers.

Hence, I focused on demonstrating this in action.

Thereby, I noticed some additional issues which should be considered.

1. `std::sqrt` should be called with value ≥0 only.

If the argument is less than -0, FE_INVALID is raised and NaN is returned.

1. Division by 0 should be checked.

2. (Due to hint of my colleague who is Dipl.-Math.) With very small values, the results of division become non-trustable.

It's in general a bad idea to compare floating point values with constants due to the usual rounding issues. Hence, I introduce an `eps` – an epsilon value to compensate at little bit.

Sample code:

```#include <cmath>
#include <iostream>

struct Quad {
double a, b, c;
Quad(double a, double b, double c): a(a), b(b), c(c) { }
Quad(const Quad&) = default;
Quad& operator=(const Quad&) = default;
~Quad() = default;

bool solve(double &root1, double &root2) const;
};

std::ostream& operator<<(std::ostream &out, const Quad &quad)
{
return out << quad.a << "x² + " << quad.b << "x + " << quad.c;
}

static double eps = 1E-10;

/* tries to solve the roots of this quadratic function.
*
* return: true (solution exists)
*         root1 and root2 contain results
*         false (no solution)
*         root1 and root2 are indeterminate (unchanged)
*/
bool Quad::solve(double &root1, double &root2) const
{
double discriminant = b * b - 4 * a * c;
if (discriminant < 0.0) return false; // square root of a negative!
if (std::abs(a) < eps) return false; // division by 0! (or close to 0)
root1 = (-b - std::sqrt(discriminant)) / (2 * a);
root2 = (-b + std::sqrt(discriminant)) / (2 * a);
return true;
}

int main()
{
double root1, root2; // declared but not yet initialized
Quad quad1(-1, 0, 1); // two solutions
std::cout << "Roots for " << quad1 << ": ";
if (!quad1.solve(root1, root2)) std::cout << "none\n";
else std::cout << root1 << ", " << root2 << '\n';
Quad quad2(1, -4, 4); // one solution
std::cout << "Roots for " << quad2 << ": ";
if (!quad2.solve(root1, root2)) std::cout << "none\n";
else std::cout << root1 << ", " << root2 << '\n';
Quad quad3(1, 0, 2); // no solution
std::cout << "Roots for " << quad3 << ": ";
if (!quad3.solve(root1, root2)) std::cout << "none\n";
else std::cout << root1 << ", " << root2 << '\n';
}
```

Output:

```Roots for -1x² + 0x + 1: 1, -1
Roots for 1x² + -4x + 4: 2, 2
Roots for 1x² + 0x + 2: none
```

Live Demo on coliru

The trick is that `Quad::solve()` returns `true` or `false` (as documented) and updates the variables `root1` and `root2` in the former case but not in the latter.

In a less compact form written:

```  bool valid = quad1.solve(root1, root2);
if (valid) { // root1, root2 updated
std::cout << root1 << ", " << root2 << '\n';
} else { // no result computed -> root1, root2 not updated
std::cout << "none\n";
}
```

I took the sample values from this German site.

7.8. Boolean Functions, We have already seen that boolean values result from the evaluation of boolean expressions. by a function (using the return statement), functions can return boolean values. Try it with a few other actual parameters to see what is printed. Stack Overflow Public questions and answers; Teams Private questions and answers for your team; Enterprise Private self-hosted questions and answers for your enterprise; Talent Hire technical talent

There is some arguing over what the return type should be, I would argue that `int` is better than `bool` because there are three different return scenarios that you might want to signal (no root, a single root, and two roots). You can argue that the latter two can be distinguished by doing a `root1 == root2` comparison in the caller, but if you have to return a success value, why not make it able to express all the options.

So my answer would be something like this:

```int Solve(double& root1, double& root2)
{
int discriminant = b * b - 4 * a * c;
if (discriminant < 0)  // No roots
return 0;
if (discriminant == 0)  // One root
{
root1 = (-b) / (2 * a);
root2 = root1;
return 1;
}
// Two roots
root1 = (-b - sqrt(discriminant)) / (2 * a);
root2 = (-b + sqrt(discriminant)) / (2 * a);
return 2;
}

int main()
{
Eq2 eq1(1, -5, 6);   //discriminant=1,root1=2,root2=3
Eq2 eq2(1, -6, 8);   //discriminant=4,root1=2,root2=4
Eq2 eq3(1, 2, 3);
Eq2 eq4(0, 0, 0);

double root1, root2;
Eq2 eqq[4] = { eq1,eq2,eq3,eq4 };
for (int i = 0; i < 4; i++)
{
eqq[i].print();
}

int numRoots = eq1.Solve(root1, root2);

switch (numRoots) {
case 0:
cout << "The the equation has no roots" << endl;
break;
case 1:
cout << "The the equation has one root: " << root1 << endl;
break;
case 2:
cout << "The the equation has two roots: " << root1 << ", " << root2 << endl;
break;
default:
cout << "Error: This should be impossible" << endl;
break;
}
system("pause");
return 0;
}
```

Functions 2: Void (NonValue-Returning) Functions, Reference parameters can: pass one or more values from function, and can change value of actual parameter; Reference parameters useful in three situations:. Functions can return values using any one of the three methods: #1) Change the state of a variable or variables. #2) Use the return command to end the function and return the supplied value to the calling section of the shell script. Example: function_name() { echo “hello \$1” return 1 } Running the function with a single parameter will echo

As suggested in the comments: "return" is an ambiguous term in this case. You want the caller to be able to obtain the roots of your second degree equation.

Since root1 and root2 are passed by reference, just by assigning their values you are providing the correct values to the caller. What you should return is true. And you should check if the discriminant is lower than 0 and return false in that case.

As @Marichyasana suggested, probably all `int`s should become `double`s.

```bool Solve(double& root1, double& root2)
{
int discrimant = b*b - 4*a*c;
if (discriminant < 0)
return false;
root1 = (-b - sqrt(discriminant))/(2*a);
root2 = (-b + sqrt(discriminant))/(2*a);
return true;
}
```

[PDF] Chapter 7 Functions, argument. The sqrt function is a value returning function, specifically the square root of the argument of the bool CheckCode(char code). { return (code = = 'T' true if the first formal parameter is greater than the second. 3. Write a value  The C++ function _____ calculates the largest whole number that is less than or equal to x. floor(x) A ________ parameter is a formal parameter that receives the location (memory address) of the corresponding actual parameter.

Function Description and Operator 'return' - Operators, If a function must not return any value, its type should be named void. As formal parameters in the function header, you can specify only variables int My_function (int a, bool b=true, int c=1, double d=0.5)//Example of function description A function in C can be called either with arguments or without arguments. These function may or may not return values to the calling functions. All C functions can be called either with arguments or without arguments in a C program. Also, they may or may not return any values. Hence the function prototype of a function in C is as below:

Signature is strange, returning list of root seems more appropriate:

```std::vector<double> SolveEq2(double a, double b, double c)
{
if (a == 0) {
throw std::runtime_error("Not equation of 2nd degree");
}
auto discriminant = b*b - 4*a*c;

if (discriminant < 0) {
return {}; // No roots;
}
else if (discriminant > 0)
{
auto sqrtDiscriminant = sqrt(discriminant);
return {(-b - sqrtDiscriminant) / (2 * a),
(-b + sqrtDiscriminant) / (2 * a)};
} else { // == 0
return {-b / (2 * a)};
}
}
```

Demo

Functions (II) - C++ Tutorials, What we did in this case was to call to function addition passing the values of x and y, is also an effective way to allow a function to return more than one value. Parameters and Arguments. Information can be passed to functions as a parameter. Parameters act as variables inside the function. Parameters are specified after the function name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma:

Function, Impure functions may return different values for the same set of parameters. function End_Of_File (file File_name: Int_Data) return BOOLEAN; The formal parameter appearing in the declaration part is the Value constant, which is a  A value-returning function can only return one value to the calling environment. The caller invokes (calls) a value-returning function by using its name and argument list in an expression (i.e., 1. assignment, 2. output, or as an 3. argument in another function call):

Chapter 7 Functions, How do you return a value from a void function in C++? The syntax for the return keyword is as follows: return [<expression>] The return keyword can appear alone, or it can be followed by a value or expression, as follows: PowerShell. return return \$a return (2 + \$a)

C++ How do I use variables from one function to another?, name and the argument list as a statement in the program. Note 2: With AddressOf we can change a Function or Sub name into an argument, a variable, that we can pass to other procedures. VB.NET program that uses AddressOf Module Module1 Function IsMatch(ByVal item As String) As Boolean Return item.Length = 4 End Function Sub Main() Dim items() As String = {"cat", "apple", "baby" } ' Use AddressOf to

##### Comments
• Please format your code to be proper C++ code
• Now, instead of declaring `bool` as a return value you should return `int`. In other words, `bool Solve(int &root1,int&root2)` should be `int Solve(int &root1,int&root2)`. Or if your function is used only for setting values `root1` and `root2`, then I suppose no return value is needed. In that case `void Solve(int &root1,int&root2)` should be the signature
• "return" is an ambiguous term in this case. Since root1 and root2 are passed by reference, just by assigning their values you are providing the correct values to the caller. What you should return is true. And you should check if the discriminant is lower than 0 and return false in that case.
• @CostantinoGrana I agree but i think OP first needs to understand what are references used for, and, furthermore, OP didn't clarify what he want exactly
• @Amanda but you are alread "returning" `root1` and `root2` because these paremeters are passed by reference.
• Nice, I didn't even consider the "`a` close to 0" case. Shows that there are always more to consider. :)