If you have an if statement & you want to return a value from inside it, then you need to make sure that both true & false conditions return a value. There are a couple ways to do that. One is to add an else clause with a return value:
float maximum(float a, float b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}
This way, no matter which path you take through the if statement, there is a return statement.
Another option is to move the return statement outside of the if statement entirely:
float maximum(float a, float b)
{
float max = b;
if (a > b)
{
max = a;
}
return max;
}
Then it doesn’t matter whether you go through the if statement, you still get to the return either way. This is my personal preference - you can come back & make changes to the function later on & you don’t have to worry about where your return statements are; there’s only one and it’s right there at the end.
A boolean only has two values, true or false. In your original code you wrote
(firstTwoAngles == 120)
firstTwoAngles was declared as a boolean, and comparing a boolean to 120 doesn’t make any sense; you compare it to either true or false.
If you want to know whether the two angles passed in to the function add up to something between 0 and 120, you would do something like
float firstTwoAngles = firstAngle + secondAngle;
if ((firstTwoAngles >= 0.0) && (firstTwoAngles <= 120.0))
{
// do something here
}
&& is a logical operator, you only use it on boolean values. In your original code you wrote
(firstAngle && secondAngle)
Since firstAngle and secondAngle are both floats, you would never use && to combine them.