函数名之后不要留空格,紧跟左括号‘(’,以与关键字区别

函数名之后不要留空格,紧跟左括号‘(’,以与关键字区别。

 

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 
 6 //参数为函数指针的函数
 7 int get_result(int a, int b, int (*sub)(int,int))
 8 {
 9     int r;
10     r=sub(a,b);
11     return r;
12 }
13  
14 //计算最大值
15 int max(int a, int b)
16 {
17     cout<<"In max"<<endl;
18     return((a > b) ? a: b);
19 }
20 
21 //计算最小值
22 int min(int a, int b)
23 {
24     cout<<"In min"<<endl;
25     return((a < b) ? a: b);
26 }
27 
28 //求和
29 int sum(int a, int b)
30 {
31     cout<<"In sum"<<endl;
32     return(a+b);
33 }
34 
35 
36 int main(int argc, char** argv) {
37     int a,b,result;
38 
39     //测试3次
40     for (int i=1;i<=3;i++) {
41         cout<<"Input a and b :";
42         cin>>a>>b;
43 
44         cout<<i<<"\tget_result("<<a<<","<<b<<", &max):"<<endl;
45         result =get_result(a, b, &max);
46         cout<<"Max of "<<a<<" and "<<b<<" is "<<result<<endl;
47    
48         result = get_result(a, b, &min);
49         cout<<"Min of "<<a<<" and "<<b<<" is "<<result<<endl;
50 
51         result = get_result(a, b, &sum);
52         cout<<"Sum of "<<a<<" and "<<b<<" is "<<result<<endl; 
53     }
54 
55     return 0;
56 }

 

posted @ 2018-08-03 11:33  borter  阅读(1388)  评论(0编辑  收藏  举报