C ++ recursion problem

I feel a little silly asking about it, but here we go ...

When trying to follow the recursion example on the following website http://www.cplusplus.com/doc/tutorial/functions2/ I ran into a problem that puzzled me.

I changed the code a bit to see the code in the recursion example, and I hugged it pretty much, but I can’t understand why the variable "n" is incremented in "Pass B" when I did not tell the program to increment "n".

Could you explain this?

#include <stdlib.h>
#include <iostream>

using namespace std;

long factorial (long n)
{
  if (n > 1)
{
   long r(0);
   cout << "Pass A" << endl;
   cout << "n = " << n << endl;
   cout << "r = " << r << endl;

   r = n * factorial (n-1);

   cout << "Pass B" << endl;  
   cout << "n = " << n << endl;
   cout << "r = " << r << endl;
   return (r);
}
  else
   return (1);
}

int main ()
{
  long number;
  cout << "Please type a number: ";
  cin >> number;
  cout << number << "! = " << factorial (number) << endl;
  system ("pause");
  return 0;
}
+3
source share
8 answers

This is because you are unrollingrecursing.

n, , n n+1, factorial(n-1)...

,

   r = n * factorial (n-1);

factorial.

( factorial n, 1), factorial, n<=1.

1, factorial, n 2, (n * whatever was returned by factorial) B r.

, , B ...

factorial(5)
  factorial(4)
    factorial(3)
      factorial(2)
        factorial(1)
        return 1
      return r
    return r
  return r
return r
+3

"Pass A" -s , "Pass B" -s , , n , n.

0

, - number = 3 ( ):

Pass A n = 3 r = 0 [1]

Pass A n = 2 r = 0 [2]

Pass B n = 2 r = 2 [3]

Pass B n = 3 r = 6 [4]

, , , , n pass B, . , n - , n, [3], n [4].

n, [1] [4], ( n = 3), , [2] [3], (.. n-1).

0

, Pass B , .

, 4, 3, 2 .., B 4 3 2 1 (.. 2 3 4)

Please type a number: 4
Pass A
n = 4
r = 0
Pass A
n = 3
r = 0
Pass A
n = 2
r = 0
Pass B
n = 2
r = 2
Pass B
n = 3
r = 6
Pass B
n = 4
r = 24
4! = 24
Press any key to continue . . .

   cout << "Pass B" << endl;  
   cout << "n = " << n << endl;
   cout << "r = " << r << " --- NOTICE RESULT ACCUMULATING AS WE UNNEST THE RECURSION" <<endl;

PS C:\temp> .\r.exe 4
Please type a number: 4
Pass A
n = 4
r = 0
Pass A
n = 3
r = 0
Pass A
n = 2
r = 0
Pass B
n = 2
r = 2 --- NOTICE RESULT ACCUMULATING
Pass B
n = 3
r = 6 --- NOTICE RESULT ACCUMULATING
Pass B
n = 4
r = 24 --- NOTICE RESULT ACCUMULATING
4! = 24
Press any key to continue . . .
0

B , "" . , "else return (1)"; . n , , , , .

0

n pass A pass B , . n-1 , n.

, , , pass A B n (n>2) .

When n=2, you'll see:
pass A for n=2
pass B for n=2

When n=3, you'll see:
pass A for n=3
pass A for n=2 // cuz of recursive call.
pass B for n=2
pass B for n=3 // looks like n has increment here..but this 'pass B' pairs with first pass A
0

.

, , n 2. "Pass A". n 1, else. , , n 2. r 2*1, n 2, .

factorial(2)
  factorial(1)
factorial(2)

n , , n , .

factorial(3)
  factorial(2)
    factorial(1)  // returns 1
  factorial(2)    // r=2*1
factorial(3)      // r=3*2
0

, , - , : :

long factorial (long n, std::string prefix= "") // default parameter
  ...
   cout prefix << "Pass A" << endl;
   cout prefix << "n = " << n << endl;
   cout prefix << "r = " << r << endl;

   r = n * factorial (n-1, prefix+ "  "); // increase indentation
   ...etc...

std::string , . .

Please type a number: 4

Pass A
n = 4
r = 0
  Pass A
  n = 3
  r = 0
    Pass A
    n = 2
    r = 0
    Pass B
    n = 2
    r = 2
  Pass B
  n = 3
  r = 6
Pass B
n = 4
r = 24

4! = 24
0

Source: https://habr.com/ru/post/1742766/


All Articles