title: Return value in a recursive function JAVA
body: <p>I have designed a function that recursively calls itself. But the return statement does not do what I want it to do. We have checked with a print that the return is reached, but it doesn't go back to the initial function.
  The statement it enters:</p>
  
  <pre><code>if(depth==0 &amp;&amp; pb.isGoalState()){
              System.out.println("!!!!!WOOOOOW!!!!!");
              return pb;
  }
  </code></pre>
  
  <p>The println shows up fine, but when pb is returned things get weird.</p>
  
  <p>When it comes back to the function:</p>
  
  <pre><code>result = DLS(pb,depth); //never returns here!!!
  System.out.println("Here: "+result.toString());
  </code></pre>
  
  <p>it never prints out the print just above. I don't see what's wrong! I have checked the other methods I designed myself.</p>
  
  <pre><code>private puzzleBoard IDS(String initial){
          puzzleBoard pb = new puzzleBoard(initial,0,new Vector&lt;Integer&gt;(),new Vector&lt;puzzleBoard&gt;(),new Vector&lt;puzzleBoard&gt;());
          int depth=0;
          puzzleBoard result=new puzzleBoard("999999999",0,new Vector&lt;Integer&gt;(),new Vector&lt;puzzleBoard&gt;(),new Vector&lt;puzzleBoard&gt;());
          while(true){//Repeat
              System.out.println("DP "+depth);
              result = DLS(pb,depth);
              System.out.println("Here: "+result.toString());
              if(result.isGoalState())
                  return result;
              depth++;
          }
  
          }
  
      private puzzleBoard DLS(puzzleBoard pb, int depth){
          System.out.println("AVskilj depth "+depth+" "+(depth==0 &amp;&amp; pb.isGoalState()));
          pb.printPuzzle();
          if(depth==0 &amp;&amp; pb.isGoalState()){
              System.out.println("!!!!!WOOOOOW!!!!!");
              return pb;
          }
          else if(depth&gt;0){
              for(Iterator&lt;puzzleBoard&gt; child = generateSuccessorsIDS(pb).iterator(); child.hasNext();){
                  puzzleBoard tmp;
                  tmp=child.next();
                  tmp.printPuzzle();
                  DLS(tmp,(depth-1));
              }
  
          }
          else
              return new puzzleBoard("999999999",0,new Vector&lt;Integer&gt;(),new Vector&lt;puzzleBoard&gt;(),new Vector&lt;puzzleBoard&gt;());
          return pb;
          }
  </code></pre>
  
  <hr>
  
  <p>So my problem is now still in this part of the code</p>
  
  <pre><code>for(Iterator&lt;puzzleBoard&gt; child = generateSuccessorsIDS(pb).iterator(); child.hasNext();){
                  DLS(child.next(),(depth-1));
              }
  </code></pre>
  
  <p>When I don't use the return before DLS(child.next(),(depth-1)); it goes through every child as intended, but doesn't store the value because of the missing return. When I use return before it it just goes through the first child in the iterator and ignores the rest, because return statements terminates for loops.</p>
  
  <p>How to solve this? I can't think of another way either.</p>