1

I'm new to C# and while coding a small function facing this weird problem. While using the return statement I want to return to the calling function but instead it takes me to a level before in the recursion and never exits from the recursive function.

My code is as follows:

private void findrel(String from, String to, List<RelAttr> relation)
    {
        String var;
        List<RelAttr> temp = new List<RelAttr>();

        for (int i = 0; i < relation.Count; i++)
        {
            if (Some Condition)
            {
                if (Another Condition)
                {
                    //Do Something
                    paths.Add(Something);
                    return;
                }
                else
                {
                    //Capture some value in var
                    paths.Add(Something);
                    //Move to Temp
                    temp.AddRange(relation);
                    //Remove Active Entry From Temp
                    temp.RemoveAt(i);
                    //Call with Newlist (temp)
                    findrel(var, to, temp);
                }
            }
        }
        //Remove Last Entry when recursion unwinds
        paths.RemoveAt(paths.Count - 1);
    }

I'm calling this function normally from the other function such as:

findrel(from, to, relations);

And I want the return statement to return to this function and not to a level before in the recursion. Any ideas?

6
  • I suspect it is returning normally and as it should: what makes you think it isn't? How are you measuring that? Commented Apr 30, 2014 at 10:22
  • Hi noelicus, when I am inside this block: if (Another Condition) { //Do Something paths.Add(Something); return; } Instead of sending me back to the calling function it comes to a lower level in recursion and continues to process it. Commented Apr 30, 2014 at 10:24
  • Code is incomplete to analyze. Lots of variables are not initialized too. Provide something concrete to analyze. BTW, did you debug? Commented Apr 30, 2014 at 10:25
  • Because you are calling the same method (recursively) in the else block. Commented Apr 30, 2014 at 10:26
  • @user2004685 my question was: how do you know? It is very unlikely what you're saying is true and it's likely whatever method you're using to assess this is misguided. Commented Apr 30, 2014 at 10:31

2 Answers 2

2

The return statement will always return to the caller, and when you are using recursion this will include returning to the level above, and will not jump back to the original caller.

If you want to get right out of findrel then you will have to do something like return a flag so that the previous level knows that it should just return back to its caller and not do any more processing.

So the code will still unwind from all the calls to findrel but you will have effectively stopped processing.

Sign up to request clarification or add additional context in comments.

2 Comments

Yup! I was thinking about the same but when I try to return an integer or boolean value it gives me a "Not all paths return value" error. I guess I have to use a global variable and then check it in the for loop before continuing.
I agree with @Ned Stoyanov, that you should be able to achieve your aim by returning a boolean from the function. A global would work, but it makes your function dependent on something outside of itself which isn't ideal.
1

Usually with recursive functions you have a terminating condition and that causes all levels of the returning function to return and this will return you to the caller. One suggestion might be to return a Boolean and terminate the recursion if that value is say false. The you can add that condition to the terminating condition of your loop and your function will exit all levels.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.