Search code examples
c++arraysfunctionfor-loopcontrol-flow

Warning about flow control in the function


I made a function to check array for negative numbers and then return the value; it takes the int testArray[] as first parameter and int n=14 as array size. I used a for loop to go though the array. I'm using an if statement to compare testArray[i]<0 and I have an else statement to print a message that no negative numbers were found. The code compiles with no errors, but I have no output. I get a warning:

In function 'int countNegative(int*, int)': 28:1: warning: control reaches end of non-void function [-Wreturn-type]

I suspect that it can be an issue with the way the parameters are passes to the function.

    #include <iostream>
    #include <cstdlib>
    using namespace std;

    int countNegative(int testArray[],int n);

    int main(){
        int testArray[] = {-2,0,44,12,-45,17,934,-21,67,88,91,1,0,6};
        int n = 14;

        countNegative(testArray,n);

        system("PAUSE");
        //EXIT_SUCCESS;
        return 0;
    }

    int countNegative(int testArray[],int n){
        for(int i=0; i<n; i++){
            if(testArray[i]<0){
                int index = testArray[i];
                return index;
            }
            else{
                cout << "No Negative Numbers";
            }
        }
    }

Solution

  • There are multiple issues with your countNegative function.

    int countNegative(int testArray[],int n){
        for(int i=0; i<n; i++){
            if(testArray[i]<0){
                int index = testArray[i]; // <= You are returning value here, not the index in the array.
                return index;
            }
            else{
                cout << "No Negative Numbers";
                // No return here, should have returned 0 ?
            }
        }
        // No return here ?
    }
    

    From the function name, it looks like it is going to count the negative values in the testArray and return the total number of negative values.

    Why are you getting this warning ?

    This is because, let's say there are no negative numbers in testArray. In such a case you do not return anything, i.e. your control can reach your else statement as well without any return value. The control may also reach the end of your function without returning any value from there. Since you have marked return type as int, you must return an integer value in all these conditions.

    If what I understand is correct, you should refactor your function to just iterate the array and count the total number of negative entries. Finally, you can return that value.

        int countNegative(int testArray[],int n){
            int total_negatives = 0;
            for(int i=0; i<n; i++){
                if(testArray[i]<0){
                    total_negatives++;
                }
            }
            if (total_negatives == 0)  cout << "No Negative numbers\n";
            return total_negatives;
        }
    

    complete-program