#include
using namespace std;
void fun(double r,double plength,double parea)
{plength=23.14r;
parea=3.14rr;}
int main()
{double r,length,area;
cin>>r;
fun(r,&length,&area);//&length传给形参plength
&area传给形参parea,函数中*plength相当于主函数的length。
cout<<length<<endl;
cout<<area<<endl;
return 0;
}
改进,增加函数的通用性,即创立一个包含函数参数的函数
#include
using namespace std;
double length(double r)
{return 23.14r;}
double area(double r)
{return 3.14rr;}
double fun(double (*f)(double),double r)
{return (*f)®;}
int main()
{double r;
cin>>r;
cout<<fun(length,r)<<endl;
cout<<fun(area,r)<<endl;
return 0;
}
double fun(double (*f)(double),double r)是包含函数参数的函数
double (*f)(double)是指向函数的指针