title: bool function for prime numbers
body: <p>I have the following code for checking whether the first 20 positive numbers are prime using a bool function.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  using namespace std;
  
  bool prime(int);
  /* 
  function to evaluate whether a positive integer is prime (true)
  or not prime (false)
  */
  int main()
  {
      for(int x=1; x&lt;=20; x++)
      {
          cout &lt;&lt;  x &lt;&lt; " a prime ? (1 yes, 0 no) "
               &lt;&lt; prime(x) &lt;&lt; endl;
      }
      return 0;
  }
  
  bool prime(int x)
  {
      for(int i=2; i&lt;= sqrt(x); i++)
      {
          if ((x%i) != 0)
              return true;
          else
              return false;
      }
  }
  </code></pre>
  
  <p>It works for all numbers <code>1 to 20</code> apart from <code>2 and 3</code> where the output is <code>0</code> instead of <code>1</code>. I think I know why. For <code>x = 2 and 3</code> there is no <code>i</code> in the <code>for</code> loop such that <code>i&lt;=sqrt(2)</code> or <code>i&lt;=sqrt(3)</code>.</p>
  
  <p>How could I modify the code so it would work for these values too? </p>
  
  <p>Also there is an error message <code>"Control may reach end of non-void function"</code>. Why is this?</p>
  
  <p>Thanks.</p>