简体   繁体   English

C递归函数不会返回true

[英]C recursive function won't return true

I have a search function which employs recursion to perform a binary search of an array, values[] , for a value : 我有其采用递归执行二进制搜索的阵列,搜索功能values[]对于一个value

int recurseSearch(int value, int values[], int min, int max) {

    if (value > values[max] || min > max) return 1;

    int midpoint = (max+min)/2;

    if (values[midpoint] > value)
        //search in left
        recurseSearch(value, values, min, midpoint);

    else if (values[midpoint] < value)
        //search in right
        recurseSearch(value, values, midpoint, max);

    else if (values[midpoint] == value)
        return 0;

    else 
        return 2;

    return 3;
}

The code that calls this simply calls recurseSearch(value, values, 0, n); 调用此代码的代码只是调用recurseSearch(value, values, 0, n);
For the sake of verifying, I will set values[5] to equal {3, 11, 32, 54, 66} , value to be 3 (ie this should return 0), and n to therefore be 5 . 为了验证起见,我将values[5]设置为等于{3, 11, 32, 54, 66}value3 (即应返回0), n5

So this gets called: recurseSearch(3, values, 0, 5); 所以这叫做: recurseSearch(3, values, 0, 5);

Now I would expect this to eventually return, and print, 0 , since 3 is indeed in the array. 现在,我希望它最终会返回并输出0 ,因为3确实在数组中。 Upon debugging everything is going well up until the midpoint is 0, and therefore values[midpoint] == value is true, and so the return 0 line should run. 调试后一切正常,直到midpoint为0,因此values[midpoint] == value为true,因此return 0行应运行。 However, what happens instead is that it does, but then comtrol aparently moves to the end (closing } ) of the function, but then moves back up and runs the return 3; 但是,发生的事情是它确实这样做了,但是后来comtrol显然移到了函数的末尾(关闭} ),然后又移回并运行了return 3; on line (here) 21. 在线(此处)21。

I cannot understand why the return 0 statement doesn't just return out of the function and why the return 3 doesn't run at all 我不明白为什么return 0语句不只是返回函数之外,为什么return 3根本不运行


NB This problem is solved by removing the return 3; 注意:此问题可通过删除return 3;来解决return 3; line, however this causes clang to complain, and the command for running ( make ) that I'm using, fatally have a hissy-fit, which I would much rather avoid 行,但是这会导致clang抱怨,并且我正在使用的运行( make )命令致命地带有嘶嘶声,我宁愿避免

I have not looked carefully at your code so there might be other bugs in it, but it sounds like you want the return value from the deepest recursive call to be passed up the stack all the way to the caller. 我没有仔细查看您的代码,因此其中可能还存在其他错误,但是听起来您希望将最深的递归调用的返回值一直传递到堆栈中,直至调用者。 In that case, you would remove the return 3; 在这种情况下,您将删除return 3; and simply return the value of each the recursive call you are making: 并简单地返回您进行的每个递归调用的值:

int recurseSearch(int value, int values[], int min, int max) {

    if (value > values[max] || min > max) return 1;

    int midpoint = (max+min)/2;

    if (values[midpoint] > value)
        //search in left
        return recurseSearch(value, values, min, midpoint);

    else if (values[midpoint] < value)
        //search in right
        return recurseSearch(value, values, midpoint, max);

    else if (values[midpoint] == value)
        return 0;

    else 
        return 2;
}

The way you originally wrote your code, the return values from the recursive calls were being ignored entirely, and the return 3; 最初编写代码的方式,递归调用的返回值被完全忽略,返回值为return 3; statement would be executed instead. 语句将代替执行。

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

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