title: what does "not all control paths return a value” mean in this program?
body: <p>I wrote a code that takes inputs until an EOF integer (999999 in this case) is typed. Then it sorts the numbers and searches for our needle (an integer) in that array of integers (called haystack). Now I am facing a problem in my searching algorithm. While compiling it shows me a warning:</p>
  
  <blockquote>
    <p>'search': not all control paths return a value</p>
  </blockquote>
  
  <p>I think it means that the function might not return anything in some cases but the problem is that i can't think of any such case. I even checked my program with a lot of values and it always gave me the correct output. Can anyone help me figure out where the problem lies. I was use some <strong>windows native tools compiler</strong> for this. </p>
  
  <p>Also if anyone has seen the <strong>CS50 pset3 find problem</strong> which is related to this, I used the same search function there too but while compiling the code in <strong>CS50 IDE</strong> the program never showed whether it found the needle in haystack. It only arranged them in increasing order and then stopped after that. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "cs50.h"
  #include &lt;stdlib.h&gt;
  
  
  bool search(int value, int values[], int n);
  void sort(int values[], int n);
  
  // maximum amount of hay
  #define MAX 65536
  
  int main(int argc, string argv[])
  {
      // ensure proper usage
      if (argc != 2)
      {
          printf("Usage: ./find needle\n");
          return -1;
      }
  
      // remember needle
      int needle = atoi(argv[1]);
  
      // fill haystack
      int size;
      int haystack[MAX];
      for (size = 0; size &lt; MAX; size++)
      {
          // wait for hay until EOF
          printf("\nhaystack[%i] = ", size);
          int straw = GetInt();
          if (straw == 999999)
          {
              break;
          }
  
          // add hay to stack
          haystack[size] = straw;
      }
      printf("\n");
  
      // sort the haystack
      sort(haystack, size);
  
      // try to find needle in haystack
      if (search(needle, haystack, size))
      {
          printf("\nFound needle in haystack!\n\n");
          return 0;
      }
      else
      {
          printf("\nDidn't find needle in haystack.\n\n");
          return 1;
      }
  }
  
  /**
   * Returns true if value is in array of n values, else false.
   */
  bool search(int value, int values[], int n)
  {
      /* TODO: implement a searching algorithm */
      int first = 0;
      int last = n;
      int middle = (first + last) / 2;
  
      while (first + 1 &lt; last)
      {
          if (value == values[middle])
          {
              return true;
              break;
          }
          if (value == values[first])
          {
              return true;
              break;
          }
          if (value == values[last])
          {
              return true;
              break;
          }        
          if (value &lt; values[middle])
          {
              last = middle;
              middle = (first + last) / 2;
          }
          if (value &gt; values[middle])
          {
              first = middle;
              middle = (first + last) / 2;
          }
      }
      if (first + 1 &gt;= last)
      {
          return false;
      }
  }
  
  /**
   * Sorts array of n values.
   */
  void sort(int values[], int n)
  {
      // TODO: implement an O(n^2) sorting algorithm
      for (int j = 1; j &lt; n; j++)
      {
          for (int i = 0; i &lt; (n - 1); i++)
          {
              if( values[i + 1] &lt; values[i])
              {
                  int b = values[i + 1];
                  values[i + 1] = values[i];
                  values[i] = b;
              }
          }
      }
      for (int k = 0; k &lt; n; k++)
      {
          printf("haystack[%d] = %d ", k, values[k]);
      }
  }
  </code></pre>