"Go" in-depth understanding of the invocation process of recursive functions

Source: Internet
Author: User

Here is an example of a simple but well-illustrated recursive invocation:

[CPP]View PlainCopy
  1. /* Recursive example */
  2. #include <stdio.h>
  3. void Up_and_down (int);
  4. int main (void)
  5. {
  6. Up_and_down (1);
  7. return 0;
  8. }
  9. void Up_and_down (int n)
  10. {
  11. printf ("Level%d:n location%p/n", n,&n); / * 1 * /
  12. if (n<4)
  13. Up_and_down (n+1);
  14. printf ("Level%d:n location%p/n", n,&n); / * 2 * /
  15. }

Output results
Level 1:n Location 0240ff48
Level 2:n Location 0240ff28
Level 3:n Location 0240ff08
Level 4:n Location 0240fee8
Level 4:n Location 0240fee8
Level 3:n Location 0240ff08
Level 2:n Location 0240ff28
Level 1:n Location 0240ff48

First, main () calls the function Up_and_down () using parameter 1, so the value of the formal parameter N in Up_and_down () is 1, so the print statement #1 output the Level1. Then, because the value of n is less than 4, Up_and_down () (level 1th) uses the parameter n+1 that is the value 2 to call Up_and_down () (level 2nd). So that n is assigned a value of 2 in the 2nd level call, the PRINT statement #1 output is Level2. Similarly, the following two calls are printed separately for LEVEL3 and Level4.

When the 4th level call is started, the value of n is 4, so the condition of the IF statement is not met. The Up_and_down () function is no longer called. The 4th level call then executes the print statement #2, which is output Level4, because the value of N is 4. Now the function needs to execute the return statement, at which point the 4th call ends, and control is returned to the function's calling function, which is the 3rd level call function. The first executed statement in the 3rd-level invocation function is a 4th-level call in the IF statement. Therefore, it continues to execute its successor code, that is, to execute the print statement #2, which will output Level3. When the 3rd-level call finishes, the 2nd-level call function starts to execute, which is output Level2. In turn.

Note that each level of recursion uses its own private variable N. You can see the value of the address to prove it.

the basic principle of recursion :

1 each function call will be returned one time. When a program flow executes at the end of a level of recursion, it moves to the previous level of recursion to continue execution.

In the 2 recursive function, the statements before the recursive call and all levels of the function are in the same order. If the print statement #1 in front of the recursive call statement, it was executed 4 times in the order of the recursive call.

3 each level of a function call has its own private variable.

In the 4 recursive function, the execution order of the statements after the recursive call statement is reversed in the order of each called function.

5 Although each level recursively has its own variables, the function code does not get copied.

6 The recursive function must contain statements that can terminate recursive calls.

Let's look at a concrete example of recursive function invocation: output integers in binary form

[CPP]View PlainCopy
  1. /* Enter an integer, Output binary form */
  2. #include <stdio.h>
  3. void To_binary (unsigned long n);
  4. int main (void)
  5. {
  6. unsigned long number;
  7. printf ("Enter an integer (Q to quit):/n");
  8. while (scanf ("%ul", &number) ==1)
  9. {
  10. printf ("Binary equivalent:");
  11. To_binary (number);
  12. Putchar ('/n ');
  13. printf ("Enter an integer (Q to quit):/n");
  14. }
  15. printf ("done./n");
  16. return 0;
  17. }
  18. void To_binary (unsigned long n)/ * Recursive function * /
  19. {
  20. int R;
  21. r=n%2; / * Calculates the value of the n%2 before the recursive call, and then outputs after the recursive invocation of the statement. This
  22. The first calculated value is instead the last output */
  23. if (n>=2)
  24. To_binary (N/2);
  25. Putchar (' 0 ' +r); /* If R is 0, the expression ' 0 ' +r is the character ' 0 '; if R is 1, the value of the expression is
  26. ' 1 '. Note that the numeric encoding of the character ' 1 ' is greater than the numeric encoding of the character ' 0 ' 1.
  27. Both the ASCII and EBCDIC codes satisfy this condition. */
  28. return;
  29. }

Output results
Enter an integer (Q to quit):
9
Binary equivalent:1001
Enter an integer (Q to quit):
255
Binary equivalent:11111111
Enter an integer (Q to quit):

From:http://blog.csdn.net/ysuncn/archive/2007/09/21/1793896.aspx

"Go" in-depth understanding of the invocation process of recursive functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.