title: C++: Is It Possible to End a Non-Void Function Without a Return?
body: <p>This is a fairly general question, but long story short, I'm trying to figure out a way to break out of a function without using return. I've got a function built from a template, so my compiler doesn't like me using "return;" to terminate early as I've seen suggested before. Here is a condensed version of what I'm trying to do:</p>
  
  <pre><code>V &amp; find (K key) {
      TreeNode&lt;K,V&gt; *traverseFind;
      //TreeNode&lt;K,V&gt; * treeNode = new TreeNode&lt;K,V&gt; (key,NULL);
      traverseFind = root;
      unsigned int compareTraverseToHeight = 0;
      //A bunch of stuff happens
      if(keyFinder == 0)  //this is what I have tried to do
          return;
      return traverseFind-&gt;value; //This is what I'm working with, the value can be NULL
  }
  </code></pre>
  
  <p>Some background: this is a find function within a binary tree, which contains two parameters key and value. A key is searched for, and then its corresponding value is returned. As part of the project, I've been tasked with throwing an exception which is caught by a tester function if a key is entered which isn't in the tree. This works fine, however, the way the tester program is configured throws me right back into the function after being resolved, in which case I have traverseFind->value = NULL, which crashes my program when I try and return it. I have no control over the tester, so I'd like to break out of the find function before I have to return a value.</p>
  
  <p>Most of this is background for the curious. My real question is: instead of the if(keyFinder ==0) function that I have tried to use (which doesn't compile), is there another option I can use to end the function early? A "break" statement for functions, so to speak?</p>
  
  <p>MORE CODE:</p>
  
  <pre><code>#ifndef __CSC116__BST_H__
  #define __CSC116__BST_H__
  
  //
  // An implementation of a binary search tree.
  //
  // This tree stores both keys and values associated with those keys.
  //
  // More information about binary search trees can be found here:
  //
  // http://en.wikipedia.org/wiki/Binary_search_tree
  //
  // Note: Wikipedia is using a different definition of 
  //       depth and height than we are using.  Be sure
  //       to read the comments in this file for the
  //   height function.
  //
  
  #include &lt;list&gt;
  #include &lt;iostream&gt;
  #include &lt;utility&gt;
  #include &lt;algorithm&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  unsigned int heightCount = 0;
  unsigned int sizeCount = 0;
  int keyFinder = 0;
  
  
  
  //
  // Each node in the tree stores
  // both a key and a value
  //
  template &lt;class K, class V&gt; class TreeNode
  {
  public:
      TreeNode(K k, V v): key(k), value(v), left(0), right(0) {}
  
      K       key;
      V       value;
      TreeNode&lt;K,V&gt;   *left;
      TreeNode&lt;K,V&gt;   *right;
      template &lt;class X, class Y&gt; friend std::ostream &amp; operator&lt;&lt; (std::ostream &amp;s, const TreeNode&lt;X,Y&gt; &amp;t);
  };
  
  // 
  // TreeNodes can output themselves to streams
  template &lt;class K, class V&gt; std::ostream &amp; operator &lt;&lt; (std::ostream &amp;s, const TreeNode&lt;K,V&gt; &amp;n)
  {
      s &lt;&lt; "\"" &lt;&lt; n.key &lt;&lt; ":" &lt;&lt; n.value &lt;&lt; "\"";
      return s;
  }
  
  //
  // This exception is thrown if you try to find 
  // a key in the tree that isn't present.
  //
  
  class key_not_found_exception {
  
  };
  
  
  
  template &lt;class K, class V&gt; class BinarySearchTree {
  public:
      //
      // Constructor
      //
      BinarySearchTree ()
      {
          sizeCount = 0;
          heightCount = 0;
          root = NULL;
      }
  
  
      ~BinarySearchTree()
      {
      }
  
  
  void insert (K k, V v) {
    TreeNode&lt;K,V&gt; * treeNode = new TreeNode&lt;K,V&gt; (k,v);
    TreeNode&lt;K,V&gt; *temp=NULL;
    TreeNode&lt;K,V&gt; *prev=NULL;
    temp = root;
  
    while(temp) { 
      prev = temp;
      if (temp-&gt;key &lt; treeNode-&gt;key)
        temp = temp-&gt;right;
      else
        temp = temp-&gt;left;
    }
  
    if (prev==NULL)
      {root = treeNode;heightCount++;}
    else {
      if (prev-&gt;key&lt;treeNode-&gt;key)
        {
          prev-&gt;right = treeNode;  
          if(prev-&gt;left ==NULL){heightCount++;}
        }
      else
        {
          prev-&gt;left = treeNode;
          if(prev-&gt;right ==NULL){heightCount++;}
        }
    } 
  
    sizeCount++;
    cout &lt;&lt; "Height of Tree is:"&lt;&lt;"_" &lt;&lt;heightCount&lt;&lt;endl;
  }
  
  
  
  
  
  
      V &amp; find (K key)                     
      {
  
      TreeNode&lt;K,V&gt; *traverseFind;
      //TreeNode&lt;K,V&gt; * treeNode = new TreeNode&lt;K,V&gt; (key,NULL);
  
      traverseFind = root; 
      unsigned int compareTraverseToHeight = 0;
  
      while (compareTraverseToHeight &lt; heightCount) 
      {
          if (traverseFind == NULL)  // We've fallen off the tree without finding item.
          {
              keyFinder = 0;
              cout&lt;&lt;"code1"&lt;&lt;endl;
              cout&lt;&lt;"keyFinder = 0"&lt;&lt;endl;
              break;
  
          }
  
          else if ( key == traverseFind-&gt;key )// We've found the item. 
          {
              keyFinder = 1;   
  
              cout&lt;&lt;"code2"&lt;&lt;endl;
              cout&lt;&lt;"keyFinder = 1"&lt;&lt;endl;
              break;
          }       
  
          else if ( key &lt; traverseFind-&gt;key )                   // If the item occurs, it must be in the left subtree,
          {                                                 // So, advance the runner down one level to the left.
              traverseFind = traverseFind-&gt;left;
  
              cout&lt;&lt;"code3";
          }   
  
  
          else  // If the item occurs, it must be in the right subtree. // So, advance the runner down one level to the right.
          {
              traverseFind = traverseFind-&gt;right;
  
              cout&lt;&lt;"code4";  
          }  
  
  
           compareTraverseToHeight++;      
  
      }  // end while
  
      cout&lt;&lt;"At end of loop, keyFinder ="&lt;&lt;keyFinder&lt;&lt;endl;
  
          try
          {
              if(keyFinder ==0)
              {
                  cout&lt;&lt;"Try has found that keyFinder = 0"&lt;&lt;endl;
                  throw key_not_found_exception();
  
              }
              else{cout&lt;&lt;"keyFinder = 1"&lt;&lt;endl;}
          }catch(...){}
  
          cout&lt;&lt;"RETURNED";
          cout&lt;&lt;traverseFind-&gt;value&lt;&lt;endl;
  
              if(keyFinder == 0)
              {
  
              }
              return traverseFind-&gt;value;
  
  
  
      }
  
  private:
  
      unsigned int doHeight (TreeNode&lt;K,V&gt; *t)
      {
          return -1;
      }
  
      void        doDelete (TreeNode&lt;K,V&gt; * n )
      {
      }
  
      TreeNode&lt;K,V&gt;   *root;
      unsigned int    count;
      unsigned int    sizeCount;
  
  #endif
  </code></pre>
  
  <p>TESTER FUNCTION</p>
  
  <pre><code>void test_insert_find()
  {
      BinarySearchTree&lt;string,string&gt; t;
  
      t.insert("bob", "bobdata");
      t.insert("joe", "joedata");
      t.insert("jane", "janedata");
  
      try
      {
          cout&lt;&lt;"Looking for Bob"&lt;&lt;endl;
          string s = t.find("bob");
          if (s != "bobdata")
              throw bst_tester_exception(__func__,__LINE__);
      }
      catch (key_not_found_exception &amp;e)
      {
              throw bst_tester_exception(__func__,__LINE__);
      }
  
      try
      {
          cout&lt;&lt;"Looking for Sarah"&lt;&lt;endl;
          string s = t.find("sarah");
          throw bst_tester_exception(__func__,__LINE__);
  
      }
  
  
      catch (key_not_found_exception &amp;e)
      {
  
          cout&lt;&lt;"Successfully caught"&lt;&lt;endl;
          // this is expected.
      }
  </code></pre>
  
  <p>FULL OUTPUT OF TEST:</p>
  
  <pre><code>Looking for Bob
  code2
  keyFinder = 1
  At end of loop, keyFinder = 1
  keyFinder = 1
  RETURNEDbobdata
  Looking for Sarah
  code4code4code1
  keyFinder = 0
  At end of loop, keyFinder = 0
  Try has found that keyFinder = 0, throwing exception
  RETURNED /*Program now crashes when it attempts to return NULL*/
  </code></pre>