CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2010
    Posts
    4

    Simple Recursion Question

    Hello,

    I'm playing around with recursive loops in order to better understand how to iterate though directories. I've used the .Net methods 'GetDirectories("*.*", SearchOption.AllDirectories)' in 'foreach' loops to iterate through the file system but many examples i've seen use recursion techniques to do the same thing. So knowing nothing of recursion I wrote the following method which should simply count to 10 to see how it works.

    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    static int value;
    static int total;

    private void Form1_Load(object sender, EventArgs e)
    {
    value = 1;
    total = Recursion(value);
    textBox1.Text += "The Total = " + total;
    }

    static int Recursion(int x)
    {
    if (x <= 10)
    {
    x++;
    Recursion(x);
    }
    return x;
    }
    }


    I assumed that the method 'Recursion(int x)' would return the value '10' to 'total' but it always returns a value of '2'. I stepped through with the debugger in VS2008 express and all goes well until 'x=10' but the method continues to loop and actually counts down before returning '2'.

    Can anyone please explain what i'm doing wrong here as i've spent several hours scratching my head over this one.

    Cheers

    SteveH

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Simple Recursion Question

    Code:
    static int Recursion(int x)
    {
      if (x < 10)
      {
        x++;
        return Recursion(x);
      } 
      return x; 
    }
    you missed the return keyword
    Last edited by dannystommen; May 7th, 2010 at 05:12 AM.

  3. #3
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Simple Recursion Question

    Code:
    static int Recursion(int x)
    {
    if (x <= 10)
    {
    x++;
    Recursion(x);
    } 
    return x; 
    }
    This code has a serious bug. If you place return statement, as suggested by Danny, it will never return.

    When you don't call return inside the if-block, it just calls the same function again, many times, until x (local variable, passed again) becomes 10. It falls back where it was called with 1 - but at that level, x is 2. Thus return value is 2.

    Please debug the code step by step!
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  4. #4
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Simple Recursion Question

    Quote Originally Posted by Ajay Vijay View Post
    If you place return statement, as suggested by Danny, it will never return.
    Why wouldn't it ever return? I tested it and it returns 10 like it should do...

  5. #5
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Simple Recursion Question

    Yes. It will return. I mis judged the code!
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured