Mutual recursion is a variation recursion. Two functions are called mutually recursive if the first function makes a recursive call to the second function and the second function, in turn, calls the first one.
In software development this concept is used in circular dependency which is a relation between two or more modules which either directly or indirectly depend on each other to function properly. Such modules are also known as mutually recursive.
A great example of mutual recursion would be implementing the Hofstadter Sequence.
In mathematics, a Hofstadter sequence is a member of a family of related integer sequences defined by non-linear recurrence relations. In this example we are going to focus on Hofstadter Female and Male sequences:
Output: F: 1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 M: 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
Disadvantages of Circular Dependency/Mutual Recursion:
- Circular dependencies can cause a domino effect when a small local change in one module spreads into other modules and has unwanted global effects
- Circular dependencies can also result in infinite recursions or other unexpected failures.
- Circular dependencies may also cause memory leaks by preventing certain very primitive automatic garbage collectors (those that use reference counting) from deallocating unused objects.
This article is contributed by Palash Nigam . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find all even length binary sequences with same sum of first and second half bits
- Print all sequences starting with n and consecutive difference limited to k
- Print all increasing sequences of length k from first n natural numbers
- Print all longest common sub-sequences in lexicographical order
- Count all sub-sequences having product <= K - Recursive approach
- Distinct strings such that they contains given strings as sub-sequences
- Count sequences of length K having each term divisible by its preceding term
- Print all non-increasing sequences of sum equal to a given number x
- Count of non-empty sequences of a String
- Practice questions for Linked List and Recursion
- Print 1 to 100 in C++, without loop and recursion
- Programs to print Triangle and Diamond patterns using recursion
- Decimal to Binary using recursion and without using power operator
- Print even and odd numbers in a given range using recursion
- Practice Questions for Recursion | Set 3
- Practice Questions for Recursion | Set 2
- Practice Questions for Recursion | Set 4
- Practice Questions for Recursion | Set 5
- Practice Questions for Recursion | Set 6
- Practice Questions for Recursion | Set 7