What does “control reaches end of non-void function” mean?

后端 未结 7 1654
野的像风
野的像风 2020-11-28 11:24

I\'ve been getting strange compiler errors on this binary search algorithm. I get a warning that control reaches end of non-void function. What does this mean?<

相关标签:
7条回答
  • 2020-11-28 11:34

    add to your code:

    "#include < stdlib.h>"
    
    return EXIT_SUCCESS;
    

    at the end of main()

    0 讨论(0)
  • 2020-11-28 11:40

    It means it's searching for a function that needs to be completed.

    else if(val == sorted[mid]) return mid;

    so, remove the if() part and modify the code or add an else() at the end which returns an int.

    0 讨论(0)
  • 2020-11-28 11:41

    I had the same problem. My code below didn't work, but when I replaced the last "if" with "else", it works. The error was: may reach end of non-void function.

    int shifted(char key_letter)
      {
            if(isupper(key_letter))
            {
                return key_letter - 'A'; 
            }
    
            if(islower(key_letter)   //<----------- doesn't work, replace with else
    
            {                                            
    
    
                return key_letter - 'a'; 
            }
    
      }
    
    0 讨论(0)
  • 2020-11-28 11:42

    Make sure that your code is returning a value of given return-type irrespective of conditional statements

    This code snippet was showing the same error

    int search(char arr[], int start, int end, char value)
    {
        int i;
        for(i=start; i<=end; i++)
        {
            if(arr[i] == value)
                return i;
        }
    }
    

    This is the working code after little changes

    int search(char arr[], int start, int end, char value)
    {
        int i;
        int index=-1;
        for(i=start; i<=end; i++)
        {
            if(arr[i] == value)
                index=i;
        }
        return index;
    }
    
    0 讨论(0)
  • 2020-11-28 11:43

    The compiler isn't smart enough to know that <, >, and == are a "complete set". You can let it know that by removing the condition "if(val == sorted[mid])" -- it's redundant. Jut say "else return mid;"

    0 讨论(0)
  • 2020-11-28 11:46

    The compiler cannot tell from that code if the function will ever reach the end and still return something. To make that clear, replace the last else if(...) with just else.

    0 讨论(0)
提交回复
热议问题