# control reaches end of non-void function

I have problem with my code (I guess). I have got the control reaches end of non-void function error even though my ifs (i think so ;) ) are covering all of the possibilities.

``````int frequency(string note)
{

int n = 0; // fraction(?) counter (n/12)
int hz = 0; //Hz of A in the Octave
int noteHz = 0;
bool specialCase = false;
float temp = 0;

//2nd step: calculate the A hrtz of octave
if (note[strlen(note) - 1] < 4)
{
for (int i = 1; 4 - i != strlen(note); i++)
hz /= 2;
}

else if(note[strlen(note) - 1] > 4)
{
for (int j = 1; 4 + j != strlen(note); j++)
hz *= 2;
}

else
hz = 440;

//!!!!THIS NEEEDS TO COME FOR AN END TO BE BETTER IMPLEMENTED - RIGHT NOW ITS BUGGY!!!!
//1st step: check if we do have special representation of notes or "normal"

if (note == '#' && note == 'B')//- this was my 1st code in this place this below (code with use of strncmp):
{
hz *=2;
specialCase = true;
}
else if (note == 'b' && note == 'A')
n++;
else if (note == '#')
n++;
else if (note == 'b')
n--;
else
n = n + 0;

if (specialCase)
{
n += 9;
temp = pow(2, n/12);
noteHz = hz / round(temp);
}

else if (note == 'A')
noteHz = hz;

else if(note == 'B' && specialCase == false)
{
n += 2;
temp = pow(2, n/12);
noteHz = hz * round(temp);
}

else if (note == 'C')
{
n += 9;
temp = pow(2, n/12);
noteHz = hz / round(temp);
}

else if(note == 'D')
{
n += 7;
temp = pow(2, n/12);
noteHz = hz / round(temp);
}

else if(note == 'E')
{
n += 5;
temp = pow(2, n/12);
noteHz = hz / round(temp);
}

else if(note == 'F')
{
n += 4;
temp = pow(2, n/12);
noteHz = hz / round(temp);
}
else if(note == 'G')
{
n += 2;
temp = pow(2, n/12);
noteHz = hz / round(temp);
}
else
n += 0;

}
``````

I hope code is clear I have some comments on it.

EDIT : 19.1.18

right now I have just changed n+= 0 for return 0 in last else statement but I have the same error and when I change an error :

error: expected expression else if (note == 'A')

If I change all `else if` for `if` the same (control reaches end of non-void function) error orrcures.

Change in the

``````else
n = n + 0; for `return 0` didnt help (still control ...)
``````

EDIT 20.1.18

after another advice from user Blauelf my code looks like this: and compiles :) Thank you

``````int frequency(string note)
{

int n = 0; // fraction(?) counter (n/12)
int hz = 440; //Hz of A in the Octave
int noteHz ;
bool specialCase = false;
float temp ;

//2nd step: calculate the A hrtz of octave
if (note[strlen(note) - 1] < 4)
{
for (int i = 1; 4 - i != strlen(note); i++)
hz /= 2;
}

else if(note[strlen(note) - 1] > 4)
{
for (int j = 1; 4 + j != strlen(note); j++)
hz *= 2;
}

else
hz = 440;

//1st step: check if we do have special representation of notes or "normal"

if (note == '#' && note == 'B')//- this was my 1st code in this place this below (code with use of strncmp):
{
hz *=2;
specialCase = true;
}
/*else*/ if (note == 'b' && note == 'A')
n++;
/*else*/ if (note == '#')
n++;
/*else*/ if (note == 'b')
n--;
else
n += 0;

if (specialCase)
{
n += 9;
temp = pow(2, n/12);
noteHz = hz / round(temp);
return noteHz;
}

else if (note == 'A')
{
noteHz = hz;
return noteHz;
}
else if(note == 'B' && specialCase == false)
{
n += 2;
temp = pow(2, n/12);
noteHz = hz * round(temp);
return noteHz;
}
else if (note == 'C')
{
n += 9;
temp = pow(2, n/12);
noteHz = hz / round(temp);
return noteHz;
}

else if(note == 'D')
{
n += 7;
temp = pow(2, n/12);
noteHz = hz / round(temp);
return noteHz;
}

else if(note == 'E')
{
n += 5;
temp = pow(2, n/12);
noteHz = hz / round(temp);
return noteHz;
}

else if(note == 'F')
{
n += 4;
temp = pow(2, n/12);
noteHz = hz / round(temp);
return noteHz;
}
else if(note == 'G')
{
n += 2;
temp = pow(2, n/12);
noteHz = hz / round(temp);
return noteHz;
}
else
return 0;
``````
• 2nd step before 1st step. Doing it right :D (don't get any of the two parts, especially how you compare character `note[strlen(note)-1]` to number 4, my attempt would be much different, but I understand why the compiler is complaining, and it definitely should) Jan 17, 2018 at 16:37
• And starting value 0 doesn't care whether you multiply or divide it by 2. Jan 17, 2018 at 16:39

This error message means "Your function is declared as `return`ing a value of type `int`, but it's possible it might `return` not at all".
Make sure control flow always ends in a `return` statement.
Have you tried `help50`? It might have told you about the same when called like `help50 make music`.
• Have you added some `return` statement, as the error message demanded? Jan 18, 2018 at 12:10