Solved

Out Parameter in Recursive Function

Posted on 2008-10-28
7
1,245 Views
Last Modified: 2013-12-17
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);
....
}

0
Comment
Question by:breezback
  • 5
7 Comments
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22827169
maybe instead use the ref key?

public func(Tree root, ref IList list)
{
.....
if (root == null)
      return;
if (list == null)
      list = new List();
func(root.child,ref list);
....
}
0
 
LVL 6

Accepted Solution

by:
RishadanPort earned 500 total points
ID: 22827171
the "out" key requires that in each function call, the "out" variable is changed.
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22827180
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 23098989
I should get credit for this question, as the answer I specified is correct.
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 23126833
Thanks
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now