Back to: C Tutorials For Beginners and Professionals

**Mutual Recursion in C Language with Examples**

In this article, I will discuss **Mutual** **Recursion in C Language** with Examples. Please read our previous article discussing **Linear** **Recursion in C Language** with Examples. At the end of this article, you will understand the following pointers:

**What is Mutual Recursion in C Language?****Multiple Real-Time Examples to Understand Mutual Recursion in C Language****Advantages of Mutual Recursion in C Language****Disadvantages of Mutual Recursion in C Language****When Would We Use Mutual Recursion in C Language?**

**What is Mutual Recursion in C Language?**

Mutual recursion in the C language, or any programming language, occurs when two or more functions call each other cyclically. This means one function calls a second function, which in turn calls the first function again, and this cycle can involve more than two functions. Mutual recursion is a form of indirect recursion but with a specific pattern of interdependency between functions.

Mutual recursion is used when the problem can be divided into subproblems of different types, each handled by a different function. These functions then rely on each other to solve the overall problem. Here’s an example to illustrate mutual recursion in C:

int isEven(int n); int isOdd(int n); int isEven(int n) { if (n == 0) return 1; // Base case for even else return isOdd(n - 1); // Call to isOdd } int isOdd(int n) { if (n == 0) return 0; // Base case for odd else return isEven(n - 1); // Call to isEven } // Calling the functions int number = 4; if (isEven(number)) printf("%d is even.\n", number); else printf("%d is odd.\n", number);

In this example, the functions isEven and isOdd are mutually recursive. isEven calls isOdd for a number decreased by 1 and isOdd calls isEven for a number decreased by 1. The base cases for each function (0 being even and not odd) break the recursion cycle. This mutual recursion continues until one of the base cases is reached.

**Mutual Recursion Real-Time Examples in C Language**

Mutual recursion occurs when two or more functions circularly call each other. This type of recursion can be useful for problems where the solution can naturally be divided into alternating tasks. Here are some examples of mutual recursion in the C language:

**Example: Odd and Even Functions**

#include <stdio.h> // Forward declarations int isOdd(int n); int isEven(int n); // Function to determine if a number is even int isEven(int n) { if (n == 0) return 1; else return isOdd(n - 1); } // Function to determine if a number is odd int isOdd(int n) { if (n == 0) return 0; else return isEven(n - 1); } int main() { int number = 5; if (isEven(number)) printf("%d is even\n", number); else printf("%d is odd\n", number); return 0; }

**Output: 5 is odd**

**Explanation:**

- isEven and isOdd are mutually recursive functions.
- isEven checks if a number is even. If n is 0, it returns 1 (true). For other values, it calls isOdd with n-1.
- isOdd checks if a number is odd. If n is 0, it returns 0 (false). For other values, it calls isEven with n-1.
- This mutual recursion continues until it reaches the base case (n == 0).

**Example: Ackermann Function**

The Ackermann function is a classic example of mutual recursion, though it’s more of a mathematical curiosity than a practical programming function due to its extremely rapid growth rate.

#include <stdio.h> // Ackermann function int ackermann(int m, int n) { if (m == 0) return n + 1; else if (n == 0) return ackermann(m - 1, 1); else return ackermann(m - 1, ackermann(m, n - 1)); } int main() { int m = 3, n = 2; printf("Ackermann(%d, %d) = %d\n", m, n, ackermann(m, n)); return 0; }

**Output: Ackermann(3, 2) = 29**

**Explanation:**

- The Ackermann function is a well-known example of a non-primitive recursive function.
- It takes two non-negative integers, m, and n.
- The recursion depends on the values of m and n.
- If m is 0, it returns n+1.
- If n is 0, it calls itself with m-1 and 1.
- Otherwise, it makes a recursive call with m-1 and a nested recursive call with m and n-1.
- This function demonstrates mutual recursion due to the nested recursive call within the third branch.

**Example: Mutual Recursion for State Machine Simulation**

Mutual recursion can simulate a simple state machine where a function represents each state. Here’s an example of a state machine with two states:

#include <stdio.h> void stateA(int count); void stateB(int count); void stateA(int count) { printf("State A: %d\n", count); if (count < 10) stateB(count + 1); // Transition to state B } void stateB(int count) { printf("State B: %d\n", count); if (count < 10) stateA(count + 1); // Transition back to state A } int main() { stateA(0); // Start with state A return 0; }

**Output:**

**Example: Mutual Recursion in Grammar Parsing**

Mutual recursion can parse certain grammatical structures where two or more grammatical rules depend on each other. Here’s a simple example:

#include <stdio.h> int isVerb(char* word); int isNoun(char* word); int isSentence(char* word) { // A sentence is a noun followed by a verb return isNoun(word) && isVerb(word + 1); } int isNoun(char* word) { // Check if the word is a noun // In a real parser, this would be more complex return strcmp(word, "cat") == 0; } int isVerb(char* word) { // Check if the word is a verb // In a real parser, this would be more complex return strcmp(word, "runs") == 0; } int main() { char* sentence[] = {"cat", "runs"}; if (isSentence(sentence)) printf("It is a sentence.\n"); else printf("It is not a sentence.\n"); return 0; }

**Output: It is not a sentence.**

**Advantages and Disadvantages of Mutual Recursion in C Language**

Mutual recursion in C language, where two or more functions call each other reclusively, offers unique advantages and challenges. This form of recursion is less common than direct recursion but can be useful in certain contexts.

**Advantages of Mutual Recursion in C Language**

**Natural Modeling of Alternating States:**Mutual recursion can be a natural way to express problems that involve alternating states or modes. For example, it’s useful in certain kinds of parsers or state machines where two states switch back and forth.**Clarity in Separating Concerns:**It allows for a clear separation of concerns by dividing a complex task into distinct functions, each handling a specific part of the problem. This can lead to more modular and maintainable code.**Simplifies Complex Logic:**In some cases, mutual recursion can simplify the logic of a problem by breaking it down into smaller, mutually recursive steps, making the overall logic easier to follow.**Elegance in Problem Solving:**Certain problems, especially in the domains of computation theory and artificial intelligence, lend themselves to elegant solutions via mutual recursion.

**Disadvantages of Mutual Recursion in C Language**

**Increased Complexity and Overhead:**Mutual recursion can make the code more complex and harder to understand, particularly for those unfamiliar with recursion. It can also lead to increased overhead due to multiple function calls.**Risk of Stack Overflow:**Like other recursive methods, mutual recursion risks stack overflow if the recursion depth becomes too large or the base cases are not properly defined.**Challenging to Debug and Maintain:**Debugging and maintaining mutually recursive functions can be more challenging than direct recursion or iterative code, especially in complex systems.**Potential for Infinite Recursion:**Without proper termination conditions, infinite recursion is likely, leading to program crashes or hangs.**Performance Concerns:**Each recursive call adds overhead. This can sometimes lead to performance issues, particularly if the recursion depth is significant or if the mutual calls are highly frequent.**Limited Applicability:**Mutual recursion is not universally applicable and is best suited for specific problems. Its use in unsuitable contexts can lead to inefficient and overly complex solutions.

**When Would We Use Mutual Recursion in C Language?**

Mutual recursion in C, or any programming language, occurs when two or more functions call each other cyclically. This form of recursion is less common than direct recursion but can be useful in specific scenarios:

**State Machine or Alternating Logic:**Mutual recursion is well-suited for implementing state machines or algorithms where the logic alternates between two states or modes. Each state or mode can be represented by a different function, with the functions calling each other to switch states.**Dividing Complex Logic into Simpler Parts:**In cases where a problem’s logic is complex, breaking it down into simpler, mutually recursive functions can make the code more manageable and easier to understand.**Parsing Tasks:**Mutual recursion can be useful in parsing, particularly in scenarios where the syntax of the language naturally leads to a structure where different parsing functions need to call each other. For instance, parsing a language that has different, interdependent grammatical rules might use mutual recursion effectively.**Mathematical Problems with Interlinked Recursions:**Certain mathematical problems or algorithms where the solution involves interlinked recursive steps can be naturally expressed using mutual recursion.

In the next article, I will discuss **Storage Classes in C Language** with Examples. In this article, I explain **Mutual Recursion in C Language** with Examples. I hope you enjoy this article on mutual recursion in C language with examples.

**About the Author: Pranaya Rout**

Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.