简体   繁体   中英

Function does not return value when it should

I wrote the following code for binary search

int binarySearch(int input[], int start, int end, int element) {
    
    if(start<=end) { 
        
        int mid = (start+end)/2;

        if(input[mid]==element) {
            cout<<"mid will be returned\n";
            return mid;       
        }

        else if(input[mid]<element) {
            binarySearch(input, mid+1, end, element);
        } else if (input[mid]>element) {
            binarySearch(input, start, mid-1, element);
        }
    }
    return -1;
}

Now, for the input

10                    //size of array
1 2 3 4 5 6 7 8 9 10  //array
1                     //element to be found

I get the output

mid will be returned
-1

But if I put an else before return -1 (in the second last line), it works just fine and returns

mid will be returned
0

The question is, shouldn't the function just return mid = 0 in the first case and return 0 ?

Here's the main function for your reference:


int main() {
    int input[100000],length,element, ans;
    cin >> length;
    for(int i =0;i<length;i++)
    { 
        cin >> input[i];;
    }

    cin>>element;
    ans = binarySearch(input, 0, length-1,  element);
    cout<< ans << endl;
}

You have to put a return before your recursive calls

int binarySearch(int input[], int start, int end, int element) {
    
    if(start<=end) { 
        
        int mid = (start+end)/2;

        if(input[mid]==element) {
            cout<<"mid will be returned\n";
            return mid;       
        }

        else if(input[mid]<element) {
            return binarySearch(input, mid+1, end, element); // here
        } else if (input[mid]>element) {
            return binarySearch(input, start, mid-1, element); // here 
        }
    }
    return -1;
}

Add return on where you calling again the function. Return will return the value to the caller function so each recursive will return the value to its caller function. So your code should be,

int binarySearch(int input[], int start, int end, int element) {
    
    if(start<=end) { 
        
        int mid = (start+end)/2;

        if(input[mid]==element) {
            cout<<"mid will be returned\n";
            return mid;       
        }

        else if(input[mid]<element) {
            return binarySearch(input, mid+1, end, element);
        } else if (input[mid]>element) {
            return binarySearch(input, start, mid-1, element);
        }
    }
    return -1;
}

Your code seems correct except the fact you don't return the result of binarySearch in recursive function.

So that gets executed but the result is not used by the original (top level) binarySearch call.

To make the main program get the right value of binarySearch method, it should call itself each time.

Change this part and try again:

else if(input[mid]<element) {
            return binarySearch(input, mid+1, end, element);
        } else if (input[mid]>element) {
            return binarySearch(input, start, mid-1, element);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM