【C語言學習】《C Primer Plus》第9章 函數

來源:互聯網
上載者:User

標籤:

學習總結

 

1、函數有利於我們可以省去重複的代碼,函數可以使程式更加模組化,從而有利於程式的閱讀、修改和完善。我們在系統設計或架構設計的時候,往往追求的是模組化、組件化、松耦合,而函數就是其代碼的表現。許多程式員喜歡把函數看作“黑盒子”,即對應一定的輸入產生特定的結果或返回某個數值,而黑盒子的內部行為並不需要考慮,然而有助於把精力投入到程式整體設計而不是其實現細節。按照C設計原則,我們不應為每個任務編寫一個單獨的函數,而應該盡量把函數的功能進行抽象設計到達通用目的。

 

2、函數的組成部分有函數類型、函數名、函數參數(形參)、函數體實現、函數返回(視函數類型而定)。如所示:

 

3、在ANSI C規範之前的傳統的函式宣告形式是不夠準確的,因為它只聲明了函數的傳回值類型,而沒有聲明其參數。如int min();這個是ANSI C之前形式的聲明通知編譯器min()返回一個int類型的數值。然而,該語句並沒有說明min()的參數個數和類型。因此,如果在函數min()中使用錯誤的參數類型和參數個數不對,編譯器就不能發現這種錯誤。

 

4、一個函數調用其本身的過程被稱為遞迴。遞迴可以代替迴圈,反之亦然。遞迴其優點在於為某些編程問題提供了最簡單的解決方案,而缺點是一些遞迴演算法會很快耗盡電腦的記憶體資源。同時,使用遞迴的程式難於閱讀和維護。

 

5、與指標相關的運算子:&、*,這兩個都是一元運算子。運算子後面跟隨一個變數,如&變數為給出該變數的地址,而這個指標地址在大多數系統內部,它是由一個不帶正負號的整數表示,但並非可以把指標看作是整數類型,一些處理整數的方法不能用來處理指標。*運算子是用來擷取儲存在被指向地址中的數值。*指標變數就是擷取該存放在該指標變數中的值。

 

6、指標也是有類型的,什麼樣的資料類型就需要什麼樣的類型指標,如整數類型指標定義:

int *pi;

這裡的星號(*)表示該變數為一指標。Pi是一個指標,而*pi是一個int類型的指標。

 

7、函數往往可以通過指標參數來改變外部的變數,如:

 1 #include <stdio.h> 2 void changeValue(int *a,int *b); 3 int main(){ 4         int a=1,b=2; 5         changeValue(&a,&b); 6         printf("a=%d,b=%d\n",a,b); 7         return 0; 8 } 9 void changeValue(int *a,int *b){10         int temp;11         temp = *a;12         *a=*b;13         *b=temp;14 }

列印結果:

a=2,b=1

 

8、編程題(題6)

 1 #include <stdio.h> 2  3 double power(double n,int p); 4  5 int main(){ 6         double x,xpow; 7         int exp; 8  9         printf("Enter a number and the positive integer power to which\n");10         printf("the number will be reduced.Enter q to quit.\n");11 12         while(scanf("%lf%d",&x,&exp)==2){13                 xpow=power(x,exp);14                 printf("%.3g to the power %d is %.5g\n",x,exp,xpow);15                 printf("Enter next pair of numbers or q to quit.\n");16         }17 18         printf("Hope you enjoyed this power trip --byte!\n");19         return 0;20 }21 22 double power(double n,int p){23         double pow=1;24         int i;25         if(n==0)26                 return 0;27         if(n==1)28                 return 1;29         for(i=1;i<=p;i++){30                 pow*=n;31         }32         return 1/pow;33 }

運行結果:

Enter a number and the positive integer power to which

the number will be reduced.Enter q to quit.

2

3

2 to the power 3 is 0.125

Enter next pair of numbers or q to quit.

4

5

4 to the power 5 is 0.00097656

Enter next pair of numbers or q to quit.

1

3

1 to the power 3 is 1

Enter next pair of numbers or q to quit.

1

5

1 to the power 5 is 1

Enter next pair of numbers or q to quit.

0

3

0 to the power 3 is 0

Enter next pair of numbers or q to quit.

0

81

0 to the power 81 is 0

Enter next pair of numbers or q to quit.

q

Hope you enjoyed this power trip --byte!

9、編程題(題7)

 1 #include <stdio.h> 2  3 double power(double n,int p,double pow); 4  5 int main(){ 6         double x,xpow; 7         int exp; 8  9         printf("Enter a number and the positive integer power to which\n");10         printf("the number will be reduced.Enter q to quit.\n");11 12         while(scanf("%lf%d",&x,&exp)==2){13                 xpow=power(x,exp,1);14                 printf("%.3g to the power %d is %.5g\n",x,exp,xpow);15                 printf("Enter next pair of numbers or q to quit.\n");16         }17 18         printf("Hope you enjoyed this power trip --byte!\n");19         return 0;20 }21 22 23 double power(double n,int p,double pow){24         if(n==0)25                 return 0;26         if(n==1)27                 return 1;28         if(p==0)29                 return n;30         if(p==1)31                 return 1/(n*pow);32         return power(n,--p,pow*n);33 }

 

【C語言學習】《C Primer Plus》第9章 函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.