简体   繁体   English

函数在应该返回值时不返回值

[英]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但是如果我在return -1之前放一个 else(在倒数第二行),它就可以正常工作并返回

mid will be returned
0

The question is, shouldn't the function just return mid = 0 in the first case and return 0 ?问题是,函数不应该在第一种情况下只返回mid = 0return 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您必须在递归调用之前return

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。 Return will return the value to the caller function so each recursive will return the value to its caller function. Return 会将值返回给调用者函数,因此每个递归都会将值返回给其调用者函数。 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.您的代码似乎是正确的,除了您没有在递归函数中返回binarySearch的结果。

So that gets executed but the result is not used by the original (top level) binarySearch call.这样就被执行了,但结果并没有被原始(顶级) binarySearch调用使用。

To make the main program get the right value of binarySearch method, it should call itself each time.为了让主程序得到binarySearch方法的正确值,它应该每次都调用自己。

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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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