Indirect Recursion & Nested Recursion in Data Structure | McCarthy 91 function | Recursion Playlist C++ | Coding With Clicks

๐Ÿ” Indirect Recursion

๐Ÿ“– Definition:

Indirect recursion occurs when two or more functions call each other in a cycle.

๐Ÿ“Œ Example in C++:

#include <iostream>
using namespace std;

void functionA(int n);
void functionB(int n);

void functionA(int n)

{
if (n > 0)

{
cout << "A: " << n << endl;
functionB(n - 1); // A calls B
}
}

void functionB(int n)

{
if (n > 0)

{
cout << "B: " << n << endl;
functionA(n / 2); // B calls A
}
}

int main()

{
functionA(5);
return 0;
}

๐Ÿง  Explanation:

  • functionA() calls functionB(), and functionB() again calls functionA().
  • This continues indirectly until the base condition stops the cycle.

๐Ÿง  These two lines:

void functionA(int n);
void functionB(int n);

are called function declarations (also known as function prototypes) in C++.

โœ… Their purpose:

They tell the compiler ahead of time that:

  • functionA and functionB exist
  • They both take an int as a parameter
  • They both return nothing (void)

This way, you can call them before their full definition appears in the code.

๐Ÿงฉ Why is it necessary here?

Because of indirect recursion.

The problem:

In the code:

void functionA(int n) 
{
functionB(n - 1); // calls functionB
}

If the compiler hasn’t seen functionB yet, it will give an error:

โŒ โ€œWhat is functionB? I havenโ€™t seen it yet.โ€

The solution:

We declare them first at the top:

void functionA(int n);
void functionB(int n);

Then we define them below.

In Short:

CodeWhat It’s Called
void functionA(int n);โœ… Function Declaration
void functionA(int n) { ... }โœ… Function Definition

๐Ÿ’ก Why Use Declarations?

  • C++ requires a function to be either defined or at least declared before it is used.
  • In your case, since functionA calls functionB, and functionB calls functionA, you need declarations before either function is defined.

๐Ÿ“ฆ Analogy:

Think of it like introducing someone before they speak:

๐Ÿ—ฃ๏ธ โ€œNext up, we have Mr. A who will talk about recursion.โ€

Now when Mr. A starts talking, everyone knows who he is. Without an introduction, they’d be confused.

๐Ÿง  Step-by-step Execution:

Letโ€™s trace the calls starting from functionA(5).

  1. functionA(5)
    • n = 5 > 0 โ†’ prints: A: 5
    • calls functionB(4)
  2. functionB(4)
    • n = 4 > 0 โ†’ prints: B: 4
    • calls functionA(2) โ†’ 4 / 2 = 2
  3. functionA(2)
    • n = 2 > 0 โ†’ prints: A: 2
    • calls functionB(1)
  4. functionB(1)
    • n = 1 > 0 โ†’ prints: B: 1
    • calls functionA(0) โ†’ 1 / 2 = 0 (integer division)
  5. functionA(0)
    • n = 0 โ†’ fails condition โ†’ stops

โœ… Final Output:

A: 5
B: 4
A: 2
B: 1

๐Ÿงฎ Why does 1 / 2 = 0 in C++?

๐Ÿ”ท Because you’re using integers, not floats.

In your code:

void functionA(int n);
void functionB(int n);
  • The parameter n is of type int (integer).
  • When you do n / 2, you’re dividing one integer by another.

๐Ÿ“Œ In integer division:

  • 1 / 2 does not give 0.5
  • It gives 0, because it drops the decimal part (truncates it)

๐Ÿงช Examples:

ExpressionTypeResult
1 / 2int0
1.0 / 2float0.5
(float)1 / 2float0.5

๐Ÿงฉ Nested Recursion

๐Ÿ“– Definition:

Nested recursion is when a functionโ€™s argument itself is a recursive call.

๐Ÿ“Œ Example in C++ (McCarthy 91 Function):

#include <iostream>
using namespace std;

int nestedRecursion(int n)

{
if (n > 100) return n - 10;
return nestedRecursion(nestedRecursion(n + 11));
}

int main() {
cout << "Result: " << nestedRecursion(95); // Output: 91
return 0;
}

๐Ÿง  Explanation:

  • The recursive call is nested inside another recursive call: return nestedRecursion(nestedRecursion(n + 11));
  • It goes deeper and deeper before resolving.

๐Ÿงฉ Nested Recursion โ€“ What does this mean?

“When a functionโ€™s argument itself is a recursive call” means:
You are passing the result of a recursive call as the input to another recursive call.

In other words, the recursion is nested inside itself.

๐Ÿ” Normal Recursion:

A function calls itself like this:

return f(n - 1);

Thatโ€™s a simple/regular recursion โ€” the function calls itself once.

๐Ÿง  Nested Recursion:

Here’s the key line:

return nestedRecursion(nestedRecursion(n + 11));

This means:

  • First, the inner call nestedRecursion(n + 11) is evaluated.
  • Then, the outer call uses that result as its argument.

So it’s like:

int temp = nestedRecursion(n + 11);   // inner call
return nestedRecursion(temp); // outer call

โžก๏ธ Thatโ€™s nested recursion: recursive calls inside recursive calls.

๐Ÿ“ Summary:

FeatureExplanation
๐Ÿ” Normal recursionFunction calls itself once directly
๐Ÿงฉ Nested recursionRecursive call is passed inside another
๐Ÿ”„ Evaluation orderInner call happens first, then outer

๐Ÿ”‘ What is Argument Passing?

In programming, argument passing means sending a value to a function when you call it.

๐Ÿง  Now, in Recursion:

Normal Recursion:

int factorial(int n) 
{
if (n == 0) return 1;
return n * factorial(n - 1); // Argument passed is (n - 1)
}

Here, factorial calls itself and passes n - 1 as the argument.

๐Ÿงฉ In Nested Recursion:

return nestedRecursion(nestedRecursion(n + 11));
  • The argument to the outer nestedRecursion(...) is the result of another recursive call: nestedRecursion(n + 11).

โžก๏ธ So instead of passing a simple value like n - 1, you’re passing the result of another function call.

๐Ÿง  What is the McCarthy 91 Function?

The McCarthy 91 function is a famous recursive function named after John McCarthy, a computer scientist and the inventor of the LISP programming language.

It is used to demonstrate nested recursion โ€” recursion where a functionโ€™s argument itself is another recursive call.

๐Ÿงฉ The Function:

Hereโ€™s the C++ version of the McCarthy 91 Function:

int mccarthy91(int n) 
{
if (n > 100)
return n - 10;
else
return mccarthy91(mccarthy91(n + 11));
}

๐Ÿ” How It Works:

  • If n > 100, it returns n - 10.
  • Otherwise, it calls itself twice, nesting the recursion:
    mccarthy91(mccarthy91(n + 11)).

๐Ÿงช Examples:

Input (n)Output
10191
10091
9591
5091

โœ”๏ธ For all n โ‰ค 100, it always returns 91.
โœ”๏ธ For n > 100, it returns n - 10.

๐Ÿคฏ Why is it interesting?

Because:

  • It looks very complex (calls itself inside itself).
  • But always returns 91 for all inputs โ‰ค 100.
  • Itโ€™s used in academic and interview settings to teach recursion and nested recursion logic.

๐Ÿ“ฆ Summary:

FeatureDescription
๐Ÿ“› NameMcCarthy 91 Function
๐Ÿ” TypeNested Recursion
๐Ÿ“Œ BehaviorAlways returns 91 for inputs โ‰ค 100
๐ŸŽ“ PurposeEducational โ€” shows deep/nested recursive logic

If you give input 120 to the McCarthy 91 function, it will return:

โœ… Output: 110

๐Ÿ“ Rule of Thumb:

Input nOutput
n > 100n - 10
n โ‰ค 100Always 91

๐Ÿ“ Summary Table:

TypeDefinitionExample Call
Indirect RecursionFunction A calls B, B calls AA() โ†’ B() โ†’ A()
Nested RecursionA recursive call is passed as an argumentf(f(n + 11))

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top