📄 411.cpp
字号:
/*
411.CPP
演示函数调用的副作用
*/
#include <iostream.h>
float SumSquare(const float a[],int num);
float SumSquare1(float a[],int num);
main()
{
const int SIZE = 5;
float f[SIZE] = { 1.1, 2.2, 3.3, 4.4, 5.5};
cout <<"Sum of Square= " << SumSquare( f ,SIZE) <<endl;
cout <<"Sum of Square1= " << SumSquare1( f ,SIZE) <<endl;//调用时有了副作用
int i;
for (i=0; i<SIZE ; i++)
cout << f[i]<<" ";//改变了原来的数据
return 0 ;
}
/* 定义函数: ↓用数组名作为参数,也可以这样说明参数类型float * a */
float SumSquare(const float a[],int num)
{
int i;
float sum = 0;
for (i=0; i<num ; i++)
{
sum += a[i]*a[i];
}
return sum;
}
float SumSquare1(float a[],int num)
{
int i;
float sum = 0;
for (i=0; i<num ; i++)
{
a[i] *= a[i];//改变了原来的数据
sum += a[i];
}
return sum;
}
/*
Sum of Square= 66.55
Sum of Square1= 66.55
1.21 4.84 10.89 19.36 30.25
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -