函数中参数妙用

来源:互联网 发布:python自动化脚本实例 编辑:程序博客网 时间:2024/06/05 20:25

在c++函数中除了返回值之外,在函数中处理参数后,再次调用参数,参数也会因为函数中的计算改变

如:

int sum(int &a,int &b)

{

a=3;

b=5;

在main函数中调用后a和b的值会改变为3,5,作用域问题消失

数组处理:

int aa(int * a)

{
 int c[]={5,4,3,2,1};
 for(int i=0;i<5;i++)
 a[i]=c[i];
 return 0;
}
int main()
{
int a1[5];
int b1=0,c=0;
c=aa(a1);
for (int i=0;i<5;i++)
cout<<a1[i]<<endl;
cout<<"\t"<<b1<<"\t"<<c<<"\t"<<endl;
}

运行结果:

sc@Power-FG:~/桌面$ g++ test.cpp -w
sc@Power-FG:~/桌面$ ./a.out
5
4
3
2
1
    0    0   

0 0
原创粉丝点击