Reputation: 4178
I'm trying to calculate the number of success cases within a recursive function in C#, but I'm astonished by the fact that my variable is shared between all the function calls!
[update 2]
More than strange this time. doing so
i = i + validTreesFun(tree.Nodes, newWords.ToList()) ;
resets i to 0
doing this
i = validTreesFun(tree.Nodes, newWords.ToList()) + i ;
gives some results (I'm not sure if it's correct)
[updated : the full code]
public static int validTreesFun(List<Tree<char>> nodes, List<string> words)
{
int i = 0;
if (nodes == null && (words == null || words.Count == 0 || (words.Count == 1 && words.First() == "")))
return 1;
else
if (nodes == null)
return 0;
foreach (Tree<char> tree in nodes)
{
var validWords = words.Where(w => w.ToCharArray()[0] == tree.Root)
.Select(w => w);
if (validWords.Count() == 0)
return 0;
else
{
var newWords = validWords.Select(w => join( w.ToCharArray().Skip(1).ToArray()));
i += validTreesFun(tree.Nodes, newWords.ToList());
}
}
return i;
}
when debuging the variable i take the value 1 but it resets to 0 on the next iteration!! despite the use of
i = i + ....
What is the problem in that piece of code?
Thank you
Upvotes: 0
Views: 3897
Reputation: 2813
Beginning from C# 7.0 you can use Local functions.
Regardless of your code, I'm answering your post's title: "Yes. It's possible to share a variable between recursive calls"
As Tree
class and join
method not provided and I didn't read your code completely, I'm not sure this works but something like this you should write:
public static int validTreesFun(List<Tree<char>> nodes, List<string> words)
{
int i = 0;
i = DoSomeOperation(nodes, words);
static int DoSomeOperation(List<Tree<char>> nodes, List<string> words)
{
// some code here
return i;
}
}
Upvotes: 0
Reputation: 965
if (validWords.Count() == 0)
return 0;
Should be
if (validWords.Count() == 0)
continue;
Also, in general, I personally think it is nicer looking to only send in one element at a time to a recursive function.
public static int validTreesFun(Tree<char> node, List<string> words)
That way you don't get the same kind of mistake like above. Finally, a minor note.
w => w.ToCharArray()[0] == tree.Root
can be written as
w => w[0] = tree.Root
Upvotes: 5
Reputation: 7719
When you are in debug mode, you indeed see that the i is reseted for the call but remain to the wanted value for the caller. Eg , the stack :
validTreesFun --i = 0 for this one
validTreesFun --i = x for this one, but if you do not go trow the calling stack, you will see 0, which is the good value for the top of the stack
Upvotes: -1
Reputation: 39956
No local variables are not at all shared between recursive calls, you should consider some other design problem, inside and after your foreach loop, I dont see any return statements, can you post full code.
Ok, in debugging you will always observe i's current method's value, debugging is not good in recursive functions, its little hard to understand, you will have to move your control down in Call Stack in order to actually observe value of earlier caller of current function.
I would advice you to output Trace or on log file with your level of node, that will help you actual debugging.
Please use TRACE Statement as follow..
Trace.WriteLine(string.Format("{0},{1}",tree.Name,i));
Upvotes: 3
Reputation: 70344
Local variables are not being shared.
What you are seeing (the reset to 0) is the value of i in the (recursively) called function validTreesFun
(i gets set to 0 at the start of the function).
Just looking at your code, I think a possible bug might be in someTestHere
- if that is never true, then i will stay 0 in the outer scope. Otherwise it should increment by 1 for each true test.
Upvotes: 1