第3章C程序计教程与实验指导杨国兴函数_第1页
第3章C程序计教程与实验指导杨国兴函数_第2页
第3章C程序计教程与实验指导杨国兴函数_第3页
第3章C程序计教程与实验指导杨国兴函数_第4页
第3章C程序计教程与实验指导杨国兴函数_第5页
已阅读5页,还剩24页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、中国水利水电出版社第3章 函数f函数调用f函数f1函数调用f2函数f2函数调用f1函数直接调用间接调用#include using namespace std;long power(int n);void main() int n; long y; cout n; y=power(n); cout n != y 1) f=n*power(n-1); else f=1; return f;第3章 函数第3章 函数abc#include using namespace std;void move(char x, char y);void hanoi(int n, char one, char tw

2、o, char three);void main() int n; cout n; cout n 个盘子的移动过程为: endl; hanoi(n, a, b, c);/函数move()将一个盘子从x针移到y针void move(char x, char y) cout x y endl;第3章 函数/函数hanoi()将n-1个盘子从one针借助two针移到three针void hanoi(int n, char one, char two, char three) if(n=1) move(one, three); else hanoi(n-1, one, three, two); mov

3、e(one, three); hanoi(n-1, two, one, three); 第3章 函数 返 回运行演示运行演示第3章 函数#include using namespace std;inline int add(int a, int b)int x;x = a+b;return x;第3章 函数 返 回void main()int a, b, c;a = 10;b = 20;c = add(a,b);cout a + b = c endl;c = add(a,50);cout a + 50 = c endl;c = add(50,50);cout 50 + 50 = c endl;

4、第3章 函数#include using namespace std;int max(int x, int y); double max(double x, double y); void main() int a=10, b=20 ,c; double x=200.3, y=400.6, z; c = max(a,b); z = max(x,y); cout c z endl; 第3章 函数 返 回int max(int x, int y) cout int function y) return x; else return y; double max(double x, double y)

5、 cout float function y) return x; else return y; 第3章 函数#include using namespace std;double power(double x=10.0, int n=2); void main() cout power(3, 5) endl; cout power(3) endl; cout power() endl; double power(double x, int n) int i;double s=1.0;for(i=1; i=n; i+) s *= x;return s; 第3章 函数第3章 函数#include

6、 using namespace std;int add(int x=5, int y=6); float add(int x=5, float y=10.0); void main() int a; float b; a= add(10,20); b= add(10); cout a= a endl; cout b= b endl; int add(int x, int y) return x+y; float add(int x, float y) return x+y; 返 回第3章 函数void f1(int a) int b,c; void main() int m,n; int i

7、,a;for(i=0; i10; i+) int b; 第3章 函数int a,b; void f1( ) int x,y;void main() #include using namespace std;int i=1; /全局变量,文件作用域 void main() cout全局变量 i=iendl; /输出1 int i=5; /函数局部变量,块作用域 int i; /块局部变量,块作用域 i=7; cout块局部变量 i=iendl; /输出7 cout全局变量 i=:iendl; /输出1,:使用全局变量 cout函数局部变量 i=iendl; /输出5 cout全局变量 i=:ie

8、ndl; /输出1,:使用全局变量 第3章 函数 第3章 函数#include using namespace std;int fact( int n);void main() int i;for(i=1; i=4; i+) cout i ! = fact(i) endl;int fact(int n) static int f=1;/仅在第一次调用函数时执行一次f *= n;return f;第3章 函数 第3章 函数#include using namespace std;void other(void); int i=1; / i 为全局变量,具有静态生存期。 void main(voi

9、d) static int a; / a为静态局部变量,具有全局寿命,局部可见。 int b=-10; / b, c为动态局部变量,具有局部生存期。 int c=0; cout-main-n; cout i: i a: a b: b c: cendl; c=c+8; other(); cout-main-n; cout i: i a: a b: b c: cendl; i=i+10; other(); 第3章 函数void other(void) / a,b为静态局部变量,具有全局寿命,局部可见,/ 只第一次进入函数时被初始化。 static int a=2; static int b; int c=10; / c为动态局部变量,每次进入函数时都初始化。 a=a+2; i

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论