Click here to Skip to main content
15,885,915 members
Please Sign up or sign in to vote.
1.00/5 (1 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

C++
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
Sergey Alexandrovich Kryukov 6-Feb-12 0:29am    
"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.
Sergey Alexandrovich Kryukov 6-Feb-12 2:14am    
Sure, I explained it in some detail :-)
--SA
Amir Mahfoozi 6-Feb-12 2:19am    
Yes I see. :)

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
 
Share this answer
 
v2
Comments
Abhinav S 6-Feb-12 1:15am    
My 5 of course.
Sergey Alexandrovich Kryukov 6-Feb-12 1:39am    
Thank you very much, Abhinav.
--SA
Amir Mahfoozi 6-Feb-12 2:20am    
+5 comprehensive answer.
Sergey Alexandrovich Kryukov 6-Feb-12 2:24am    
Thank you, Amir.
--SA
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);
   ...
}
 
Share this answer
 
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900