breezback
asked on
Out Parameter in Recursive Function
Hi,
I have a function that adds elements from tree to a list recursively.
I would like to put the list initialization inside the implementation of the function (There's no good reason, just to understand how)
and pass the list argument as out parameter.
The thing is when I do it I get the error saying I must assign value, because I only initialize the list the first time if it is null the else condition doesn't have initialization.
How should I write it.
Here's the pseudo code:
public func(Tree root, out IList list)
{
.....
if (root == null)
return;
if (list == null)
list = new List();
func(root.child,out list);
....
}
I have a function that adds elements from tree to a list recursively.
I would like to put the list initialization inside the implementation of the function (There's no good reason, just to understand how)
and pass the list argument as out parameter.
The thing is when I do it I get the error saying I must assign value, because I only initialize the list the first time if it is null the else condition doesn't have initialization.
How should I write it.
Here's the pseudo code:
public func(Tree root, out IList list)
{
.....
if (root == null)
return;
if (list == null)
list = new List();
func(root.child,out list);
....
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I should get credit for this question, as the answer I specified is correct.
Thanks
public func(Tree root, ref IList list)
{
.....
if (root == null)
return;
if (list == null)
list = new List();
func(root.child,ref list);
....
}