Triangle Challenge

I have this error that keeps popping up: “control may reach end of non-void function”.

Firstly, what does that mean? What would happen if control does reach outside of the function? What kind of control are we talking about?

Secondly How do I fix it?

thanks a lot for your time (-;

The triangle function must return a float, but the return statement only executes if the if statement condition evaluates to true. If the condition is false, there is no return value. That’s what the error is complaining about.

Also, firstTwoAngles is a bool, but that if statement is doing some decidedly non-boolean things with it. And firstAngle and secondAngle are both floats, so &&-ing them together doesn’t really make sense.

I am very happy to see that you are learning the C language, but I find the code quite frightening.

Can you write down clearly, in plain language, what you are trying to express with the following statements?

bool firstTwoAngles = ...

if (!(firstTwoAngles == 120) && (firstTwoAngles - 0)) {
    ...
}

If you do that, I will help you clean up the code :slight_smile:

ok. firstly thanks alot for reaching out to help me.

my goal for this program is to calculate the third angle of a triangle -assuming you know the first two.
all the angles of a triangle must add up to 180.
with bool i make sure that the program will work only when the first two angles do not exceed the value of 120 and they dont go lower than 0.
than with the if statement i make sure that my bool variable is true, thus assigning third angle the the value of the third angle which is the value of totalAngel minus firstAngle and secondAngle.

i rewrote this code it does the same thing as the one were discussing:
i hope thats clear and thanks again

Hello thanks for answering my question. i rewrote the code and here it is.

i understand what your saying abt the function returning a float but not being able to because the if statement may be false
hence ‘control MAY exceed…’
how would i be able to keep this same code but fix that problem?

i do not understand abt the second thing that you said. what do you mean but that if statement is doing some decidedly non-boolean things with it.’ with this if statement im evaluating whether bool var is true or not so whats non bool abt it?

secondly what does a var being a float have to do with &&??

thanks a lot for your support and help!!

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.

JonAult has already touched on quite a few things.

To add to his, I have the following code for your perusal.

// Type alias
typedef float angle_type;

// Function declaration
angle_type triangle (const angle_type, const angle_type);

// Driver
int main (int argc, const char * argv []) {
    const angle_type r1 = triangle (80, 80);
    printf ("%f\n", r1);
    
    const angle_type r2 = triangle (80, 100);
    printf ("%f\n", r2);

    return 0;
}

// Function definition
angle_type triangle (const angle_type firstAngle, const angle_type secondAngle) {
    const angle_type sumOf3Angles = 180;
    
    // Invariant
    assert (firstAngle  > 0);
    assert (secondAngle > 0);
    
    const angle_type sumOf2Angles = firstAngle + secondAngle;
    
    // Invariant
    assert (sumOf2Angles < sumOf3Angles);
    
    // Result
    return sumOf3Angles - sumOf2Angles;
}

The code does not use an if statement. Instead, it uses assert statements to kill the program if an invariant is violated.

Also, note the use of the type alias for float.

typedef float angle_type;

This makes it easier to document the triangle function.

Compare the following two declarations.

float triangle (const float, const float);
angle_type triangle (const angle_type, const angle_type);