blitzeus
blitzeus

Reputation: 495

C++ Recursion stack

I'm confused how Recursion works in this example. If 'ABC\n' is inputted, CBA is outputted. If someone could step through the process I would greatly appreciate it.

  1. In main(), ReverseLine() is called
  2. local automtic myInput takes in 'ABC\n'

  3. Then it checks myInput for '\n' and EOF, this is where I start getting confused

I think it says, A != '\n' and A != EOF so ReverseLine() is called again, but then what???

How does the recursion work, I just want to understand the process

THANKS

    using namespace std;

    void ReverseLine(){
       int myInput;

       myInput = cin.get();

       if (myInput != '\n' && myInput != EOF)
        ReverseLine();

       if (myInput != EOF)
        cout.put(myInput);
    }

    int main(){

       ReverseLine();
       return 0;

    }

Upvotes: 1

Views: 570

Answers (4)

m0skit0
m0skit0

Reputation: 25873

Recursion, as Basile said, can be hard to understand. This example relies on the local variable concept. It will go to the end of the recursivity layer, and then start printing the local variable myInput from deepest recursive call to the first.

Let's suppose you input "123". Each indentation is a new local scope for ReverseInput().

myInput = 1
ReverseLine()
  myInput = 2
  ReverseLine()
    myInput = 3
    ReverseLine()
      myInput = \n
    prints 3
  prints 2
prints 1

This is a common trick to do things the reverse way.

Upvotes: 2

itsbruce
itsbruce

Reputation: 4843

It's really very simple. The ReverseLine function prints its output before it returns. Here is the sequence of events, if *ABC\n is typed in

1. First call to ReverseLine.
1.a **A** is typed.
1.b myInput is not equal to **\n or EOF**, so
   2. Second call to ReverseLine
   2.a **B** is typed.
   2.b myInput is not equal to **\n** or **EOF**, so
      3. Third call to ReverseLine
      3.a **C** is typed.
      3.b myInput is not equal to **\n** or **EOF**, so
         4. Fourth call to ReverseLine
         4.a **\n** is typed.
         4.b myInput is equal to **\n**, so
         4.c ReverseLine is **not** called
         4.d myInput is **not** equal to **EOF**, so
         4.e myInput (**\n**) is printed
         4.f Fourth call to ReverseLine returns
      3.c myInput is **not** equal to **EOF**, so
      3.d myInput (**C**) is printed
      3.e Third call to ReverseLine returns
   2.c myInput is **not** equal to **EOF**, so
   2.d myInput (**B**) is printed
   2.e Second call to ReverseLine returns
1.c myInput is **not** equal to **EOF**, so
1.d myInput (**A**) is printed
1.e First call to ReverseLine returns

And then the program ends.

Upvotes: 1

Darius Makaitis
Darius Makaitis

Reputation: 880

When you call ReverseLine, it reads a character. If the character isn't a newline or EOF, it calls itself again (recurses) to read the next character until it encounters a newline at which point it prints the character it just read, then returns to ReverseLine which prints the character it read and so forth until it returns to the initial call to ReverseLine, prints the first character read, then exits.

Upvotes: 4

Pubby
Pubby

Reputation: 53067

Perhaps expanding it will help you understand?

void ReverseLine() {
   int myInput = 'a'

   if (myInput != '\n' && myInput != EOF) {
       int myInput = 'b'

       if (myInput != '\n' && myInput != EOF) {
           int myInput = 'c'

           if (myInput != '\n' && myInput != EOF) {
               int myInput = '\n'
               if (myInput != '\n' && myInput != EOF)
                   ReverseLine(); // doesn't get called
               cout.put(myInput);
           }
           if (myInput != EOF)
               cout.put(myInput);
       }

       if (myInput != EOF)
        cout.put(myInput);
   }

   if (myInput != EOF)
    cout.put(myInput);
}

Upvotes: 4

Related Questions