【轉】深入理解遞迴函式的調用過程

來源:互聯網
上載者:User

標籤:

下面是個關於遞迴調用簡單但是很能說明問題的例子:

[cpp] view plain copy 
  1. /*遞迴例子*/  
  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. }  

 

輸出結果
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

 

 首先, main() 使用參數 1 調用了函數 up_and_down() ,於是 up_and_down() 中形式參數 n 的值是 1, 故列印語句 #1 輸出了 Level1 。然後,由於 n 的數值小於 4 ,所以 up_and_down() (第 1 級)使用參數 n+1 即數值 2 調用了 up_and_down()( 第 2 級 ). 使得 n 在第 2級調用中被賦值 2, 列印語句 #1 輸出的是 Level2 。與之類似,下面的兩次調用分別列印出 Level3 和 Level4 。

 

 當開始執行第 4 級調用時, n 的值是 4 ,因此 if 語句的條件不滿足。這時候不再繼續調用 up_and_down() 函數。第 4 級調用接著執行列印語句 #2 ,即輸出 Level4 ,因為 n 的值是 4 。現在函數需要執行 return 語句,此時第 4 級調用結束,把控制權返回給該函數的調用函數,也就是第 3 級調用函數。第 3 級調用函數中前一個執行過的語句是在 if 語句中進行第 4 級調用。因此,它繼續執行其後繼代碼,即執行列印語句 #2 ,這將會輸出 Level3 .當第 3 級調用結束後,第 2 級調用函數開始繼續執行,即輸出Level2 .依次類推.

 注意,每一級的遞迴都使用它自己的私人的變數 n .可以查看地址的值來證明.

 

遞迴的基本原理

 

1 每一次函數調用都會有一次返回.當程式流執行到某一級遞迴的結尾處時,它會轉移到前一級遞迴繼續執行.

2 遞迴函式中,位於遞迴調用前的語句和各級被調函數具有相同的順序.如列印語句 #1 位於遞迴調用語句前,它按照遞迴調用的順序被執行了 4 次.

3 每一級的函數調用都有自己的私人變數.

4 遞迴函式中,位於遞迴調用語句後的語句的執行順序和各個被調用函數的順序相反.

5 雖然每一級遞迴有自己的變數,但是函數代碼並不會得到複製.

6 遞迴函式中必須包含可以終止遞迴調用的語句.

 

再看一個具體的遞迴函式調用的例子:以二進位形式輸出整數

 

[cpp] view plain copy 
  1. /*輸入一個整數,輸出二進位形式*/  
  2. #include<stdio.h>  
  3. void to_binary(unsigned long n);  
  4.   
  5. int main(void)  
  6. {  
  7.     unsigned long number;  
  8.     printf("Enter an integer(q to quit):/n");  
  9.     while(scanf("%ul",&number)==1)  
  10.     {  
  11.         printf("Binary equivalent :");  
  12.         to_binary(number);  
  13.         putchar(‘/n‘);  
  14.         printf("Enter an integer(q to quit):/n");  
  15.     }  
  16.     printf("Done./n");  
  17.     return 0;  
  18.       
  19. }  
  20. void to_binary(unsigned long n)    /*遞迴函式*/  
  21. {  
  22.     int r;  
  23.     r=n%2;    /*在遞迴調用之前計算n%2的數值,然後在遞迴調用語句之後進行輸出.這樣 
  24.           計算出的第一個數值反而是在最後一個輸出*/  
  25.     if(n>=2)  
  26.         to_binary(n/2);  
  27.         putchar(‘0‘+r);/*如果r是0,運算式‘0‘+r就是字元‘0‘;如果r是1,則運算式的值為 
  28.                                 ‘1‘.注意前提是字元‘1‘的數值編碼比字元‘0‘的數值編碼大1. 
  29.                  ASCII和EBCDIC這兩種編碼都滿足這個條件.*/  
  30.         return;  
  31. }   

 

輸出結果
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

【轉】深入理解遞迴函式的調用過程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.