# Need help with recursive binary search

Here is my helpers.c file, I have been scouring the message boards and nothing is making sense. I use the pset3 input of ./generate 1000 50 | ./find 127. I get a Didn't find needle in haystack message. Please help. I apologize for the poor format, this is my first time posting to stackexchange.

``````/**
* helpers.c
*
* Computer Science 50
* Problem Set 3
*
* Helper functions for Problem Set 3.
*/

#include <cs50.h>
#include "helpers.h"

bool BinarySearch(int key, int array[], int min, int max);
/**
* Returns true if value is in array of n values, else false.
*/
bool search(int value, int values[], int n)
{
return BinarySearch(value, values, n-n, n-1);

}

/**
* Sorts array of n values.
*/
void sort(int values[], int n)
{
int i, element, j;
for(i=1;i <n;i++)
{
element = values[i];
j=i;
while (j > 0 && values[j-1] > element)
{
values[j]=values[j-1];
j=j-1;
}
values[j]=element;

}

// TODO: implement an O(n^2) sorting algorithm
return;
}

bool BinarySearch(int key, int array[], int min, int max)
{
//find midpoint
int midpoint = (max-min)/2;

// check if input is valid

if(min > max || midpoint > max || midpoint < min)
{
return false;
}

//check if midpoint=key
if(array[midpoint]==key || array[min] == key||array[max]==key)
{
return true;
}else if(array[midpoint] < key) // check if midpoint is larger than the key
{
BinarySearch(key, array, midpoint+1, max);
}else if(array[midpoint] > key) //check if midpoint is larger than key
{
BinarySearch(key, array, min, midpoint -1);
}
return false;

}
``````

When you call a recursive function, you actually need to deal with the return value of that function. You are currently just calling `BinarySearch(key, array, min, midpoint-1)` for example and then throwing away whatever value it returns.
Be sure to `return BinarySearch(...)` whenever you call it.
Also, look again at your `int midpoint` value. If, for example, your max is 10 and your min is 8, should your mid be (10-8)/2 = 1? That's outside the range of what you are searching. Shouldn't the mid be 9 in that case? Perhaps just a typo in your equation?