Solved

Out Parameter in Recursive Function

Posted on 2008-10-28
7
1,259 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

22 Experts available now in Live!

Get 1:1 Help Now