Key Takeaway

Mutual Recursion: A programming technique where two or more functions call each other. It's essential to ensure that mutual recursion progresses towards a base case to avoid infinite recursion and non-terminating programs.

Summary: Mutual Recursion

Definition

Example: Even and Odd Functions

isEven Function:

int isEven(unsigned int n) {
  if (n == 0) {
    return 1;  // Base case
  }
  if (n == 1) {
    return 0;  // Base case
  }
  return isOdd(n - 1);  // Call isOdd for complex step
}

isOdd Function:

int isOdd(unsigned int n) {
  if (n == 0) {
    return 0;  // Base case
  }
  if (n == 1) {
    return 1;  // Base case
  }
  return isEven(n - 1);  // Call isEven for complex step
}

Complete Example with Prototypes:

int isOdd(unsigned int n);  // Prototype for isOdd

int isEven(unsigned int n) {
  if (n == 0) {
    return 1;
  }
  if (n == 1) {
    return 0;
  }
  return isOdd(n - 1);
}

int isOdd(unsigned int n) {
  if (n == 0) {
    return 0;
  }
  if (n == 1) {
    return 1;
  }
  return isEven(n - 1);
}

Practical Uses

Example:

f(3, g(42), h(x, y, z(1)))