C++ 第3章 函数和作用域.ppt_第1页
C++ 第3章 函数和作用域.ppt_第2页
C++ 第3章 函数和作用域.ppt_第3页
C++ 第3章 函数和作用域.ppt_第4页
C++ 第3章 函数和作用域.ppt_第5页
已阅读5页,还剩33页未读 继续免费阅读

下载本文档

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

文档简介

1、第3章 函数和作用域,3.1 函数的定义和说明 3.2 函数的调用 3.3 函数的参数传递 3.4 内联函数 3.5 带默认参数的函数 3.6 函数重载 3.7 使用C+系统函数 3.8 作用域,【 3.1 函数的定义和说明】,【 3.1.1 函数的定义】,声明函数,就是告诉编译器函数的名称、类型和形式参数。,定义函数,就是告诉编译器函数所做的工作。,在C+程序中,定义一个函数的格式如下: 类型 函数名(形式参数表) 语句序列 ,形式参数表由0个、1个或多个参数组成,内容如下: 类型1 形式参数名1,类型2 形式参数名2,类型n 形式参数名n,例 3.1,声明函数,一般采用声明函数原型。形式如

2、下: 类型 函数名(形式参数表);,例如: double rectanglearea(double a,double b); double rectanglearea(double,double);,【 3.2 函数的调用】,【 3.1.2 函数的声明】,函数的调用的一般形式如下: 函数名(实际参数表),函数调用:,函数的返回值是通过返回语句return来实现的。 return语句的一般格式如下: return 表达式;,例 3.2,double rectanglearea(double a, double b); /函数声明 void main() double length, width;

3、 coutlengthwidth; cout the area of the rectangle is rectanglearea (length, width); / length和width是实参,分别将各自的值赋给形参a和b coutendl; double rectanglearea(double a, double b) / a和b是形参 double s; s = a * b; return s; ,int a = 20; int s; s = area(+a, a * 2); coutsendl; int area(int a, int b) return (a * b); ,例

4、 3.4,【 3.3 函数的参数传递 】,cout x= x t y = yendl; swap(x, y); cout In main function: endl; cout After swap: ; cout x= x t y = yendl; void swap(int a, int b) int temp; cout In Swap function: endl; cout Before swap: ; cout a= at b= bendl; temp = a; a = b; b = temp; cout After swap: ; cout a= a t b= bendl; ,

5、【 3.4 内联函数】,形式如下: inline 类型 函数名(形参表) ./函数体 ,【 3.5 带默认形参值的函数】,例如: double area(double a=2.6,double b=4); double area(double a,double b=4); double area(double=2.6,double =4);,double area(double a=2.6,double b); double area(double =2.6,double b,double=4);,int area; area = volume(l, w, h); coutendlthe ar

6、ea of cube equal:t area; coutendlendl; area = volume(l, w); /h取默认的值 coutendl the area of cube equal:tarea; coutendlendl; area = volume(l); /w和h取默认的值 coutendl the area of cube equal:tarea; coutendlendl; int volume(int length, int width, int height) cout The information of the cube is:tlength=t length

7、; cout twidth=twidth theight=t height; return (length*width*height); ,例 3.8,if(number=2) coutlength2width2; coutendl; total = totalarea(area(length1, width1), area(length2, width2); cout The sum of the two rectangless area is : totalendl; else total = totalarea(area(length1, width1); cout The sum of

8、 the rectangles area is : totalendl; int area(int a,int b) return (a*b); int totalarea(int a,int b) return (a + b); ,【3.6 函数重载】,例如:int fun(int, int); int fun(int); long fun(int, long); long fun(long);,例如:int fun1(int x, int y); long fun1(int x, int y);,例如:int fun(int a,int b); int fun(int x,int y);,

9、例 3.9,int add(int x, int y) cout In int add(int x, int y) endl; coutx+y=; return(x + y); /end of int add long add(long x, long y) cout In long add(long x, long y) endl; cout x + y =; return(x + y); /end of long add float add(float x, float y) cout In float add(float x, float y) endl; cout x + y =; r

10、eturn (x + y); /end of float add double add(double x, double y) cout In double add(double x, double y) endl; coutx+y=; return (x + y); /end of double add,例如:int fun(int a,int b=0); int fun(int a);,【 3.7 使用C+系统函数】,例 3.10, x = (x1 + x2) / 2.0; if (fabs(x + sqrt(x) - 2.8) 0) x2 = x; else x1 = x; ,【 3.8

11、 作用域】,【 3.8.1 作用域分类】,例如: double area(double x);,例如: void fun() int a; /a的作用域起始处 cina; if(a0) int b; / b的作用域起始处 . / b的作用域结束处 / a的作用域结束处,if语句,可以在语句中进行条件测试的表达式内声明标识符。,例如: if(int i=f() /标识符i的作用域起始处 i=i*2; else i=100; /标识符i的作用域结束处 couti; /错误,标识符i在其作用域外不可见,例如: for(int i=0;i5;) /标识符i的作用域起始处 i+; /标识符i的作用域结束处 couti; /错误,标识符i在其作用域外不可见,3. 函数作用域,例如:void fun() double a; int a; .; ,例如:void fun() int a; int a; .; ,在不同作用域内,允许声明同名标识符。,例如:void fun() double a(3); .; int

温馨提示

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

评论

0/150

提交评论