title: Return inside loop for C++ breaks loop?
body: <p>I am reversing a doubly linked list. My function for the same is :</p>
  
  <pre><code>Node* Reverse(Node* head)
  {
      // Complete this function
      // Do not write the main method. 
      Node* temp = new Node();
      if ( head == NULL) { return head; }
      while ( head != NULL) {
          temp = head-&gt;next;
          head-&gt;next = head-&gt;prev;
          head-&gt;prev = temp;
          if (temp == NULL ) { break; }
          head = temp;
      }
      return head;
  }
  </code></pre>
  
  <p>This works correctly.
  Instead of using the break command, if I do  'return head' than the function exits the while loop and has a compile error: control reaches end of non-void function [-Werror=return-type]</p>
  
  <pre><code>Node* Reverse(Node* head)
  {
      // Complete this function
      // Do not write the main method. 
      Node* temp = new Node();
      if ( head == NULL) { return head; }
      while ( head != NULL) {
          temp = head-&gt;next;
          head-&gt;next = head-&gt;prev;
          head-&gt;prev = temp;
          if (temp == NULL ) { return head; }
          head = temp;
      }
   }
  </code></pre>
  
  <p>What could be the reason behind this?</p>