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

后端 未结 7 1656
野的像风
野的像风 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: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;
    }
    

提交回复
热议问题