例41 编写一个函数cube,计算整数的立方。调用函数 Cube计算从1到10相邻整数的立方差 2005-4-28 北京邮电大学电信工程学院计算机技术中心
2005-4-28 北京邮电大学电信工程学院计算机技术中心 -26- 例4.1 编写一个函数cube,计算整数的立方。调用函数 cube计算从1到10相邻整数的立方差
计算整数的立方 #include <iostream> using namespace std int cube(int);∥函数原型声明 void maino int last, nwcb: last=l cout <<the difference of cube: <<endl 2005-4-28 北京邮电大学电信工程学院计算机技术中心 27
2005-4-28 北京邮电大学电信工程学院计算机技术中心 -27- //计算整数的立方 #include <iostream> using namespace std; int cube( int ); // 函数原型声明 void main() { int last,nowcb; last=1; cout <<"the difference of cube: "<<endl;
for(int x=2; X<=10; x++) nwcb=cube(x) cout <<nowcb-ast < last=nwcb cout < endl: 2005-4-28 北京邮电大学电信工程学院计算机技术中心
2005-4-28 北京邮电大学电信工程学院计算机技术中心 -28- for ( int x = 2; x <= 10; x++ ) { nowcb=cube( x ); cout <<nowcb-last << " "; last=nowcb; } cout << endl; }
/函数定义 int cube( int y return y“v y 2005-4-28 北京邮电大学电信工程学院计算机技术中心
2005-4-28 北京邮电大学电信工程学院计算机技术中心 -29- //函数定义 int cube( int y ) { return y*y*y; }
例42 在三个浮点中确定最大值,使用自定义函数 maximum完成。 2005-4-28 北京邮电大学电信工程学院计算机技术中心
2005-4-28 北京邮电大学电信工程学院计算机技术中心 -30- 例4.2 在三个浮点中确定最大值,使用自定义函数 maximum完成