Click here to Skip to main content
14,334,621 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello everybody!

I have a question about recursion.

I have 2 functions called f1 and f2. In f1's body we call f2 function, in f2's body we call f1 function, like this

return_type1 f1(parameter list 1)
{
   f2(parameter list 2);
   ...
}

return_type2 f2(parameter list 2)
{
   f1(parameter list 1);
   ...
}


But it didn't work. I think there must be some stuff (like keyword) so that the compiler would know a bout this type of recursion.

Any help would be appreciated!
Posted
Updated 5-Feb-12 18:17pm
v2
Comments
   
"Did not work" is not informative.
--SA
Amir Mahfoozi 6-Feb-12 0:54am
   
If you add finish conditions to it then it will work well and If you don't consider adding end conditions it will never work. No need for any special keyword.
   
Sure, I explained it in some detail :-)
--SA
Amir Mahfoozi 6-Feb-12 2:19am
   
Yes I see. :)
Rate this:
Please Sign up or sign in to vote.

Solution 2

There is no such keyword, simply because recursion is not a problem.

In the past, in some computer languages recursion was not allowed. Later on, recursion made its way into some of those programming languages, and some authors of some languages, if I'm not much mistaken, really introduced a special keyword to indicate where recursion is allowed.

However, pretty soon some clever programmers manages to convince the authors of those languages that they have been complete idiots because recursion is not a problem if used by a clever programmer. Those authors pretended that they were clever, too, but in fact felt ashamed and stopped to invent such stupid computer languages.

Since that time, recursion become a commonplace, is widely used, and probably no one is going to use those obsolete programming languages where recursion is not allowed or limited.

The bugs related to recursion are possible, but they are one of the most easiest to detect, because stack overflow exception is thrown. Finding of the source of the problem is one of the most easy debugging tasks known in the history of programming.

Now, please see my comment to the question. "Not working" is not informative; and the pseudo-code sample you demonstrated is quite legitimate. This is called mutual recursive, please see http://en.wikipedia.org/wiki/Mutual_recursion[^].

You could screw up implementation somehow. One possible reason is "infinite" recursion. You need to define a condition for the end of recursion, otherwise you will get the stack overflow. To bad you did not share description of your problem and did not create any code sample. So, you will have to debug the code by yourself.

This is easy — please see above. Just use the Debugger.

Good luck,
—SA
   
v2
Comments
Abhinav S 6-Feb-12 1:15am
   
My 5 of course.
   
Thank you very much, Abhinav.
--SA
Amir Mahfoozi 6-Feb-12 2:20am
   
+5 comprehensive answer.
   
Thank you, Amir.
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 3

Besides all the good advice in Solution 1 and Solution 2, if by "it doesn't work" you mean "it doesn't compile", that would be because in the example shown, you need to declare a forward reference to "f2". Straight C needs that in order to resolve the function calls.

return_type2 f2(parameter list 2);

return_type1 f1(parameter list 1)
{
   f2(parameter list 2);
   ...
}
 
return_type2 f2(parameter list 2)
{
   f1(parameter list 1);
   ...
}
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

There is no keyword.
Try debugging (stepping through) your code to see if you can trace flow from function f1 into function f2 and from f2 into f1.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100