您的位置:首页 > 编程语言 > C语言/C++

C++之函数简介

2015-02-09 00:21 295 查看
主要知识点:
·函数基本知识
·函数原型
·如何按值传递函数参数
·如何设计处理数组的函数
·如何使用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 }


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: