C++之函數簡介

主要知識點:

·函數基本知識

·函數原型

·如何按值傳遞函數參數

·如何設計處理數組的函數

·如何使用const指針參數

·如何設計處理文本字符串的函數

·如何設計處理結構的函數

·如何設計處理String類對象的函數

·調用自身的函數(遞歸)

·指向函數的指針

1.函數的基本知識

#1.要使用c++函數,必須完成的工作:

(1)提供函數定義  void funcation(){...}

(2)提供函數原型  如 int  funcation(); 

(3)調用函數

複製代碼
 1 //函數定義,原型,調用
 2 #include<iostream>
 3 using namespace std;
 4 void simple();
 5 
 6 int main()
 7 {
 8     cout<<"main() will call the simple() function;\n";
 9     simple();
10     return 0;
11 }
12 
13 void simple()
14 {
15     cout<<"I'm but a simple function.\n";
16 }
17 */!:這個程序很簡單,但是用最簡單的方式闡述了函數的用法,包括三個完整的過程,函數原型、函數定義和函數調用,當然函數的定義出現在函數調用之前的話,則不必要進行函數原型的說明,只是C++習慣將主函數放在程序的開始
18 */
複製代碼

#2.函數定義:將函數分爲兩類,有返回值的和沒有返回值的函數(稱爲void函數),對於有返回值的函數必須使用返回語句(如  return)。

  函數返回的值可以是常量、變量,也可以是表達式但是其結果必須是相應的類型。c++返回值限制不能是數組,但是卻可以把數組作爲結構或對象的組成部分來返回。

#3.函數原型:原型描述了函數到編譯器的接口,即將函數返回值的類型以及參數的類型和數量告訴編譯器。

複製代碼
 1 //函數原型
 2 #include<iostream>
 3 using namespace std;
 4 void cheers(int);
 5 double cube(double x);
 6 
 7 int main(void)
 8 {
 9     cheers(5);
10     cout<<"Give me a number:";
11     double side;
12     cin>>side;
13     double volume=cube(side);
14     cout<<"A "<<side<<"-foot cube has a volume of ";
15     cout<<volume<<" cubic feet\n";
16     cheers(cube(2));
17     return 0;
18 }
19 void cheers(int n)
20 {
21     for(int i=0;i<n;i++)
22         cout<<"CHEERS!";
23     cout<<endl;
24 }
25 
26 double cube(double x)
27 {
28     return x*x*x;
29 }
30 /*!:設計了兩個函數,並說明了函數的傳值調用,現在你會區分什麼是形參什麼是實參了麼?賦值的是實參,接受賦值的那傢伙是形參記住不要搞錯了。
31 */
複製代碼

(1)爲什麼需要原型    如 double  v=f(side);

  首先,原型告訴編譯器,f()有一個double類型的參數,若沒有則編譯器不捕獲這樣的錯誤。

  其次,f()函數完成計算後,將返回值放在指定位置(可能是CPu寄存器或者是內存中)。

  最後,調用函數從這個位置取得返回值。

(2)原型句法   ex:  vod funcation(int x);或者  void funcation(int);

  函數原型不要求提供變量名,但是類型列表是必要的。

(3)原型功能 

  @編譯器正確處理函數返回值。

  @編譯器檢查使用的參數數目是否正確。

  @編譯器檢查使用的參數類型是否正確。   如果類型不匹配,那麼如果有意義時將發生強制類型轉換。

~~~d對於c來說函數原型是可選的,但c++中原型是不可選的。

2.函數參數和按值傳遞

#1.在函數中聲明的變量(包括參數)是該函數私有的,在函數被調用時,計算機爲變量分配內存,函數結束時則釋放這些變量使用的內存~局部變量的誕生。

#2.在函數中修改形參的值不會影響調用程序中的數據。

複製代碼
 1 //多個參數
 2 #include<iostream>
 3 using namespace std;
 4 void n_chars(char,int);
 5 int main(void)
 6 {
 7     int times;
 8     char ch;
 9     cout<<"Enter a character:";
10     cin>>ch;
11     //cout<<"Enter a integer:";
12     //cin>>times;
13     while(ch!='q')
14     {
15         cout<<"Enter a integer:";
16         cin>>times;
17         n_chars(ch,times);
18         cout<<"\nEnter another character or press the q-key to quit:";
19         cin>>ch;
20     }
21     cout<<"The value  of time is "<<times<<".\n";
22     cout<<"Bye\n";
23     return 0;
24 }
25 
26 void n_chars(char c,int n)
27 {
28     while(n-->0)
29         cout<<c;
30 }
複製代碼
複製代碼
 1 //一個重要的計算中獎概率的函數
 2 #include<iostream>
 3 using namespace std;
 4 
 5 long double probalility(unsigned numbers,unsigned picks);
 6 int main(void)
 7 {
 8     double total,choices;
 9     cout<<"Enter the total number of choices on the game card\nand the number of picks allowed:\n";
10     while((cin>>total>>choices)&&choices<=total)
11     {
12         cout<<"You have one chance in "<<probalility(total,choices)<<" of winning.\n";
13         cout<<"Next two numbers(q to quit):\n";
14     }
15     return 0;
16 }
17 
18 long double probalility(unsigned numbers,unsigned picks)
19 {
20     long double result=1.0;
21     long double n;
22     unsigned p;
23     for(n=numbers,p=picks;p>0;p--,n--)
24         result=result*n/p;
25     return result;
26 }
27 //!:在函數設計中,計算部分運用了交叉乘除,這樣可以很好的避免數據過大。
複製代碼

3.函數和數組
#1.如何處理數組 ex: int sum(int arr[],int n)//爲使函數通用而不限於特定長度的數組,還需要傳遞數組的長i度。arr實際上並不是數組,而是一個指針。函數頭int sum_arr(int arr[],int n)裏的數組,可以證明在函數頭和函數原型中int arr[ ]和int *arr的含義是相同的,他們都意味着arr是一個int指針。還有這裏需要說明,數組和普通變量在函數調用過程中的區別,變量時做了一個拷貝改變不影響實參的值,而數組卻還是原來的數組,要不改變數組的值怎麼辦呢?用const。

複製代碼
 1 //數組的函數處理,求和
 2 #include<iostream>
 3 using namespace std;
 4 const int Arsize = 8;
 5 int sum_arr(int arr[],int n);
 6 
 7 int main(void)
 8 {
 9     int cookies[Arsize]={1,2,3,4,5,6,7,8};
10     int sum=sum_arr(cookies,Arsize);
11     cout<<"Total cookies eatem: "<<sum<<endl;
12     return ;
13 }
14 
15 int sum_arr(int arr[],int n)
16 {      
17     int total=0;
18     for(int i=0;i<n;i++)
19         total=total+arr[i];
20     return total;
21 }
複製代碼

#2.兩個恆等式

@~1   arr[i]==*(ar+i)  //values  in two notations

@~2  &arr[i]==ar+i   //addresses in two notations

~將指針(包括數組名)加1,實際上是加上了一個與指針指向的類型的長度(以字節爲單位)相等的值i,對於遍歷數組而言,使用指針加法和數組下標是等價的。

#3.將數組作爲函數參數意味着在傳遞數組時,函數將使用原來的數組。將數組地址作爲參數可以節省複製整個數組所需要的時間和內存,同時也增加了破壞原始數組的風險。

複製代碼
 1 //關於數組的重要討論
 2 //在用函數處理數組的時候,記住用兩個參數來分別傳遞數組和數組的長度
 3 //int sum_arr(int arr[],int n)
 4 //不要試圖使用下列方法
 5 //int sum_arr(int arr[size])
 6 
 7 #include<iostream>
 8 using namespace std;
 9 const int Arsize = 8;
10 int sum_arr(int arr[],int n);
11 
12 int main(void)
13 {
14     int cookies[Arsize]={1,2,3,4,5,6,7,8};
15     cout<<cookies<<"=array address,"<<sizeof cookies<<"=sizeof cookies\n";
16     
17     //sizeof cookies是數組的長度,sizeof arr只是指針變量的長度
18     int sum=sum_arr(cookies,Arsize);
19     cout<<"Total cookies eaten:"<<sum<<endl;
20     
21     //可以對數組撒謊,告訴他你只有3個元素
22     sum=sum_arr(cookies,3);
23     cout<<"Total cookies eaten: "<<sum<<endl;
24     cout<<"first three eaters ate "<<sum<<" cookies."<<endl;
25     
26     //爲數組指明假的開始地址
27     //cookies+4是第五個元素的地址,相當於&cookies[4]
28     sum=sum_arr(cookies+4,4);
29     cout<<"Last  four eaters ate "<<sum<<" cookies."<<endl;
30     return 0;
31 }
32 
33 int sum_arr(int arr[],int n)
34 {      
35     int total=0;
36     cout<<arr<<"=arr ,";
37     cout<<sizeof arr<<" =sizeof arr"<<endl;
38     for(int i=0;i<n;i++)
39         total=total+arr[i];
40     return total;
41 }
複製代碼
複製代碼
 1 //模塊化的數組函數
 2 #include<iostream>
 3 using namespace std;
 4 const int Max=5;
 5 
 6 //(1)填充數組
 7 //函數的作用是,輸入一個數組,並且返回實際輸入數組的長度
 8 int fill_array(double ar[],int limit)
 9 {
10     double temp;
11     int i;
12     for(i=0;i<limit;i++)
13     {
14         cout<<"Enter value#"<<(i+1)<<":";
15         cin>>temp;
16         //////////////////////////////
17         if(!cin)
18         {
19             cin.clear();
20             while(cin.get()!='\n')
21                 continue;
22             cout<<"Bad input: input process terminated."<<endl;
23             break;
24         }
25         ////////////////////////////////
26         else if(temp<0)//當輸入值爲非負數的時候,這個值將被複制給數組,否則循環結束
27             break;
28         ar[i]=temp;
29     }
30     //循環完成的最後一項工作是把i的值加1,此時i比最後一個數組的索引大1,所以等於填充元素的數目
31     return i;
32 }
33 
34 //(2)顯示數組及用const保護數組
35 void show_array(const double ar[],int n)
36 {
37     for(int i=0;i<n;i++)
38     {
39         cout<<"property #"<<(i+1)<<":";
40         cout<<ar[i]<<endl;
41     }
42 }
43 
44 //(3)修改數組
45 void revalue(double r,double ar[],int n)
46 {
47     for(int i=0;i<n;i++)
48         ar[i]*=r;
49 }
50 
51 //主函數
52 int main()
53 {
54     double properties[Max];
55     int size=fill_array(properties,Max);
56     show_array(properties,size);
57     cout<<"Enter revaluation factors:";
58     double factors;
59     cin>>factors;
60     revalue(factors,properties,size);
61     show_array(properties,size);
62     cout<<"Done."<<endl;
63     return 0;
64 }
複製代碼

#4.可以使用數組區間來處理數組元素。

複製代碼
 1 //使用數組區間的函數
 2 #include<iostream>
 3 using namespace std;
 4 const int Arsize=8;
 5 
 6 int sum_arr(const int *begin,const int *end);
 7 int main()
 8 {
 9     int cookies[Arsize]={1,2,4,8,16,32,64,128};
10     int sum=sum_arr(cookies,cookies+Arsize);
11     cout<<"Total cookies eaten: "<<sum<<endl;
12     sum=sum_arr(cookies,cookies+3);
13     cout<<"First three eaters ate "<<sum<<" cookies."<<endl;
14     sum=sum_arr(cookies+4,cookies+8);
15     cout<<"Last four eaters ate "<<sum<<" cookies."<<endl;
16     return 0;
17 }
18 
19 
20 int sum_arr(const int *begin,const int *end)
21 {
22     const int * pt;
23     int total=0;
24     for(pt=begin;pt!=end;pt++)
25         total=total+*pt;
26     return total;
27 }
複製代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章