




已阅读5页,还剩345页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第1页 第2页 第3章 函数 3.1 函数的定义与调用 3.2 函数参数的传递 3.3 函数调用机制 3.4 函数指针 3.5 内联函数和重载函数 3.6 变量存储特性与标识符作用域 3.7 多文件结构程序 3.8 命名空间 3.9 终止程序执行 小结 第3页 第3章 函数 函数(Function)是功能抽象的模块 函数 参数 返回值 输入流 输出流 第4页 第3章 函数 函数(Function)是功能抽象的模块 函数作用 任务划分;代码重用 函数是C+程序的重要组件 第5页 函数定义由两部分组成:函数首部和函数操作描述 函数调用是通过表达式或语句激活并执行函数代码的过程 3.1 函数的定义和调用 / 求圆柱体体积 #include using namespace std ; double volume ( double radius, double height ) return 3.14 * radius * radius * height ; int main() double vol, r, h ; cin r h ; vol = volume ( r, h ) ; cout using namespace std ; double volume ( double radius, double height ) return 3.14 * radius * radius * height ; int main() double vol, r, h ; cin r h ; vol = volume ( r, h ) ; cout using namespace std ; double volume ( double radius, double height ) return 3.14 * radius * radius * height ; int main() double vol, r, h ; cin r h ; vol = volume ( r, h ) ; cout y ) return x ; else return y ; 3.1.1 函数定义 第20页 例3-2 3.1.1 函数定义 double max ( double x , double y ) if ( x y ) return x ; else return y ; 函数返回值类型 3.1.1 函数定义 第21页 例3-2 3.1.1 函数定义 double max ( double x , double y ) if ( x y ) return x ; else return y ; 函数名 3.1.1 函数定义 第22页 例3-2 3.1.1 函数定义 double max ( double x , double y ) if ( x y ) return x ; else return y ; 形式参数表 3.1.1 函数定义 第23页 例3-2 3.1.1 函数定义 double max ( double x , double y ) if ( x y ) return x ; else return y ; 函数体 3.1.1 函数定义 第24页 例3-2 3.1.1 函数定义 double max ( double x , double y ) if ( x y ) return x ; else return y ; 返回值 return 语句形式: returnreturn 表达式 或returnreturn (表达式 ) 作用: 返回函数值 不再执行后续语句,程序控制返回调用点 一个函数体内可以有多个return 语句 表达式 返回值的类型与函数类型不相同时, 自动强制转换成函数的类型 3.1.1 函数定义 第25页 例3-2 3.1.1 函数定义 double max ( double x , double y ) if ( x y ) return x ; else return y ; TypeType FunctionName () / statements returnreturn expressionexpression ; voidvoid FunctionName () / statements returnreturn ; /可省略 3.1.1 函数定义 第26页 调用形式 函数名 ( 实际参数表 ) 3.1.2 函数调用 3.1.2 函数调用 第27页 调用形式 函数名函数名 ( 实际参数表 ) 3.1.2 函数调用 函数名函数名 函数的入口地址 3.1.2 函数调用 第28页 调用形式 函数名 ( 实际参数表实际参数表 ) 3.1.2 函数调用 函数名 函数的入口地址 实际参数表实际参数表 与形式参数必须在个数、类型、位置一一对应 3.1.2 函数调用 第29页 调用形式 函数名 ( 实际参数表 ) 3.1.2 函数调用 函数名 函数的入口地址 实际参数表 与形式参数必须在个数、类型、位置一一对应 3.1.2 函数调用 用表达式或语句形式调用用表达式或语句形式调用; ; 若函数返回值类型为若函数返回值类型为voidvoid,则只能用语句调用,则只能用语句调用 第30页 3.1.2 函数调用 #include using namespace std ; void printmessage () cout using namespace std ; void printmessage () cout using namespace std ; double max ( double x , double y ) if ( x y ) return x ; else return y ; int main() double a, b; cin a b ; double m = max( a, b ); cout using namespace std ; double max ( double x , double y ) if ( x y ) return x ; else return y ; int main() double a, b; cin a b ; double m = max( a, b ); cout using namespace std ; double max ( double x , double y ) if ( x y ) return x ; else return y ; int main() double a, b; cin a b ; double m = max( a, b ); cout using namespace std ; double max( double, double ) ;/ 函数原型 int main() double a, b, c, m1, m2 ; cout a b c ; m1 = max( a, b ) ;/ 函数调用 m2 = max( m1, c ) ; cout y ) return x ; else return y ; 使用函数原型 3.1.3 函数原型 第38页 3.1.3 函数原型 #include using namespace std ; double max( double, double ) ;/ 函数原型 int main() double a, b, c, m1, m2 ; cout a b c ; m1 = max( a, b ) ;/ 函数调用 m2 = max( m1, c ) ; cout y ) return x ; else return y ; 函数原型的参数表 不需要参数名 使用函数原型 3.1.3 函数原型 第39页 3.1.3 函数原型 #include using namespace std ; double max( double, double ) ;/ 函数原型 int main() double a, b, c, m1, m2 ; cout a b c ; m1 = max( a, b ) ;/ 函数调用 m2 = max( m1, c ) ; cout y ) return x ; else return y ; 函数调用出现在定义之前 函数原型声明是必须的 使用函数原型 3.1.3 函数原型 第40页 3.1.3 函数原型 函数定义在调用之前 #include using namespace std ; double max( double x, double y )/ 函数定义 if ( x y ) return x ; else return y ; int main() double a, b, c, m1, m2 ; cout a b c ; m1 = max( a, b ) ;/ 函数调用 m2 = max( m1, c ) ; cout using namespace std ; double max( double x, double y )/ 函数定义 if ( x y ) return x ; else return y ; int main() double a, b, c, m1, m2 ; cout a b c ; m1 = max( a, b ) ;/ 函数调用 m2 = max( m1, c ) ; cout #include using namespace std ; int main() double PI = 3.1415926535; double x, y; x = PI/ 2; y = sin( x ); cout #include using namespace std ; int main() double PI = 3.1415926535; double x, y; x = PI / 2; y = sin( x ); cout #include using namespace std ; int main() double PI = 3.1415926535; double x, y; x = PI / 2; y = sin( x ); cout using namespace std ; int main ( ) double add1 ( double , double ) ; / 函数原型 double add2 ( int , int ) ; / 函数原型 double a , b, c ; cin a b ; c = add1 ( a , b ) ; cout using namespace std ; int main ( ) double add1 ( double , double ) ; / 函数原型 double add2 ( int , int ) ; / 函数原型 double a , b, c ; cin a b ; c = add1 ( a , b ) ; cout using namespace std ; int add(int , int ) ; int main() int a, b, c ; cin a b; c = add(a,b) ; cout using namespace std ; int add(int , int ) ; int main() int a, b, c ; cin a b; c = add(a,b) ; cout using namespace std ; int add(int , int ) ; int main() int a, b, c ; cin a b; c = add(a,b) ; cout a b; / 例3-5 值参传递 1 1值传递机制值传递机制 3.2.1 传值参数 第54页 #include using namespace std ; int add(int , int ) ; int main() int a, b, c ; cin a b; c = add(a,b) ; cout using namespace std ; int add(int , int ) ; int main() int a, b, c ; cin a b; c = add(a,b) ; cout using namespace std ; int add(int , int ) ; int main() int a, b, c ; cin a b; c = add(a,b) ; cout using namespace std ; int add(int , int ) ; int main() int a, b, c ; cin a b; c = add(a,b) ; cout using namespace std ; int add(int , int ) ; int main() int a, b, c ; cin a b; c = add(a,b) ; cout using namespace std ; int add(int , int ) ; int main() int a, b, c ; cin a b; c = add(a,b) ; cout using namespace std ; int add(int , int ) ; int main() int a, b, c ; cin a b; c = add(a,b) ; cout using namespace std ; int add(int , int ) ; int main() int a, b, c ; cin a b; c = add(a,b) ; cout using namespace std ; double CylinderVolume( double r, double h ); double DonutSize(double Outer, double Inner, double Height); int main() double OuterRadius, InnerRadius, Height; cout OuterRadius ; cout InnerRadius ; cout Height ; cout using namespace std ; double CylinderVolume( double r, double h ); double DonutSize(double Outer, double Inner, double Height); int main() double OuterRadius, InnerRadius, Height; cout OuterRadius ; cout InnerRadius ; cout Height ; cout using namespace std ; double CylinderVolume( double r, double h ); double DonutSize(double Outer, double Inner, double Height); int main() double OuterRadius, InnerRadius, Height; cout OuterRadius ; cout InnerRadius ; cout Height ; cout using namespace std ; double CylinderVolume( double r, double h ); double DonutSize(double Outer, double Inner, double Height); int main() double OuterRadius, InnerRadius, Height; cout OuterRadius ; cout InnerRadius ; cout Height ; cout using namespace std ; double CylinderVolume( double r, double h ); double DonutSize(double Outer, double Inner, double Height); int main() double OuterRadius, InnerRadius, Height; cout OuterRadius ; cout InnerRadius ; cout Height ; cout using namespace std ; int add ( int x , int y ) return x + y ; int main ( ) int x = 4 , y = 6 ; int z = add ( + x , x + y ) ; cout using namespace std ; int add ( int x , int y ) return x + y ; int main ( ) int x = 4 , y = 6 ; int z = add ( + x+ x , x + yx + y ) ; cout using namespace std ; int add ( int x , int y ) return x + y ; int main ( ) int x = 4 , y = 6 ; int z = add ( + x+ x , x + yx + y ) ; cout using namespace std ; int add ( int x , int y ) return x + y ; int main ( ) int x = 4 , y = 6 ; int z = add ( + x+ x , x + yx + y ) ; cout using namespace std ; int add ( int x , int y ) return x + y ; int main ( ) int x = 4 , y = 6 ; int z = add ( + x+ x , x + yx + y ) ; cout using namespace std ; int add ( int x , int y ) return x + y ; int main ( ) int x = 4 , y = 6 ; int z = add ( + x+ x , x + yx + y ) ; cout using namespace std ; int add ( int x , int y ) return x + y ; int main ( ) int x = 4 , y = 6 ; int z = add ( + x+ x , x + yx + y ) ; cout using namespace std ; int add ( int x , int y ) return x + y ; int main ( ) int x = 4 , y = 6 ; int z = add ( + x+ x , x + yx + y ) ; cout using namespace std ; double power ( double real, int n = 2 ) ; int main ( ) double r = 3.0 ; cout using namespace std ; double power ( double real, int n = 2int n = 2 ) ; int main ( ) double r = 3.0 ; cout using namespace std ; double power ( double real, int n = 2 ) ; int main ( ) double r = 3.0 ; cout using namespace std ; double power ( double real, int n = 2int n = 2 ) ; int main ( ) double r = 3.0 ; cout using namespace std ; void swap ( int * , int * ) ; int main () int a = 3 , b = 8 ; cout using namespace std ; void swap ( int *int * , int *int * ) ; int main () int a = 3 , b = 8 ; cout using namespace std ; void swap ( int *int * , int *int * ) ; int main () int a = 3 , b = 8 ; cout using namespace std ; void swap ( int *int * , int *int * ) ; int main () int a = 3 , b = 8 ; cout using namespace std ; void swap ( int *int * , int *int * ) ; int main () int a = 3 , b = 8 ; cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void func ( int * p ) ; int main() int x = 20 ; func( cout using namespace std ; void swap ( int *int * , int *int * ) ; int main () int a = 3 , b = 8 ; cout using namespace std ; void swap ( int *int * , int *int * ) ; int main () int a = 3 , b = 8 ; cout using namespace std ; void swap ( int *int * , int *int * ) ; int main () int a = 3 , b = 8 ; cout using namespace std ; void swap ( int int main () int a = 3 , b = 8 ; cout using namespace std ; void swap ( int int main () int a = 3 , b = 8 ; cout using namespace std ; void swap ( int int main () int a = 3 , b = 8 ; cout using namespace std ; void swap ( int int main () int a = 3 , b = 8 ; cout using namespace std ; #include void display( const int display( x ) ; display( 4589 ) ; 3.2.3 引用参数 第124页 3.2.3 引用参数 用 const 约束引用参数 / 例3-9 不同数制输出正整数 #include using namespace std ; #include void display( const int display( x x ) ; display( 4589 ) ; 在实参对象上 只读访问 3.2.3 引用参数 第125页 3.2.3 引用参数 用 const 约束引用参数 / 例3-9 不同数制输出正整数 #include using namespace std ; #include void display( const int display( x x ) ; display( 45894589 ) ; 常引用 产生匿名对象 只有常引用对应的实参可 以是常量或表达式 非约束的引用参数对应的 实参必须是对象名 3.2.3 引用参数 第126页 3.2.3 引用参数 用 const 约束引用参数 / 例3-9 不同数制输出正整数 #include using namespace std ; #include void display( const int display( x ) ; display( 4589 ) ; 3.2.3 引用参数 第127页 3.2.3 引用参数 / 例3-10 常引用参数的匿名对象测试 #include using namespace std ; void anonym ( const int void anonym ( const int void anonym ( const int double volume ( double , double ) ; int main() double vol, r, h ; cin r h ; vol = volume ( r, h ) ; cout using namespace std ; double volume ( double , double ) ; int main() double vol, r, h ; cin r h ; vol = volume ( r, h ) ; cout using namespace std ; double volume ( double , double ) ; int main() double vol, r, h ; cin r h ; vol = volume ( r, h ) ; cout using namespace std ; double volume ( double , double ) ; int main() double vol, r, h ; cin r h ; vol = volume ( r, h ) ; cout using namespace std ; double volume ( double , double ) ; int main() double vol, r, h ; cin r h ; vol =vol = volume ( r, h ) ; cout using namespace std ; double volume ( double , double ) ; int main() double vol, r, h ; cin r h ; vol = volume ( r, h ) ; cout using namespace std ; int * maxPoint(int * x, int * y ) ; int main() int a, b ; cout a b ; cout *y ) return x ; return y ; 返回过程: 对地址表达式求值 int * maxPoint(int * x, int * y ) ; int main() int a, b ; cout a b ; cout *y ) return x ; return y ; 返回过程: 对地址表达式求值 int * maxPoint(int * x, int * y ) ; int main() int a, b ; cout a b ; cout *y ) return x ; return y ; 返回过程: 对地址表达式求值 int * maxPoint(int * x, int * y ) ; int main() int a, b ; cout a b ; cout *y ) return x ; return y ; 返回过程: 对地址表达式求值 int * maxPoint(int * x, int * y ) ; int main() int a, b ; cout a b ; cout *y ) return x ; return y ; int * f1Warning() int temp = 100 ; return int main() cout using namespace std ; int * f1Warning() int temp = 100 ; return int main() cout using namespace std ; int cout a b ; cout y ) return x ; return y ; 2010 ab x x y y 返回过程: 决定引用对象 3.2.4 函数的返回类 型 第146页 3 3返回返回引用引用 / 例3-13 返回较大值变量的引用 #include using namespace std ; int cout a b ; cout y ) return x ; return y ; 2010 ab x x y y 返回过程: 决定引用对象 匿名对象绑定 Obj 3.2.4 函数的返回类 型 第147页 3 3返回返回引用引用 / 例3-13 返回较大值变量的引用 #include using namespace std ; int cout a b ; cout y ) return x ; return y ; 2010 ab x x y y 返回过程: 决定引用对象 匿名对象绑定 返回调用点,执行输出操作 Obj 3.2.4 函数的返回类 型 第148页 3 3返回返回引用引用 / 例3-13 返回较大值变量的引用 #include using namespace std ; int cout a b ; cout y ) return x ; return y ; 2010 ab x x y y 返回过程: 决定引用对象 匿名对象绑定 函数返回 对象 a 的引用 返回调用点,执行输出操作 Obj 3.2.4 函数的返回类 型 第149页 3 3返回返回引用引用 / 例3-13 返回较大值变量的引用 #include using namespace std ; int cout a b ; cout y ) return x ; return y ; 2010 ab x x y y 返回过程: 决定引用对象 匿名对象绑定 返回调用点,执行输出操作 撤销匿名对象和形参对象的引用 Obj 3.2.4 函数的返回类 型 第150页 3 3返回返回引用引用 / 不应该返回局部量的引用 #include using namespace std ; int return temp; int main() cout using namespace std ; int return temp; int main() cout using namespace std ; long fact ( int m ) int i ; long sum = 1 ; for ( i = 1 ; i a b ; f1 = fact ( a ) / ( fact ( b ) * fact ( a-b ) ) ; cout using namespace std ; long fact ( int m ) int i ; long sum = 1 ; for ( i = 1 ; i a b ; f1 = fact ( a ) / ( fact ( b ) * fact ( a-b ) ) ; cout using namespace std ; long fact ( int m ) int i ; long sum = 1 ; for ( i = 1 ; i a b ; f1 = fact ( a ) / ( fact ( b ) * fact ( a-b ) ) ; cout using namespace std ; long fact ( int m ) int i ; long sum = 1 ; for ( i = 1 ; i a b ; f1 = fact ( a ) / ( fact ( b ) * fact ( a-b ) ) ; cout using namespace std ; int Factorial ( int ) ; int main () int k ; cout k ; cout using namespace std ; int Factorial ( int ) ; int main () int k ; cout k ; cout using namespace std ; int Factorial ( int ) ; int main () int k ; cout k ; cout using namespace std ; void reverse () char ch ;/ 局部量 cin ch; if ( ch != . ) reverse() ; cout using namespace std ; void reverse () char ch ;/ 局部量 cin ch; if ( ch != . ) reverse() ; cout using namespace std ; void reverse ( int n ) cout 0 ) : “ k ; reverse ( k ) ; cout using namespace std ; void hanoi ( int n, char a, char b, char c ) if ( n = 1 ) cout “ “ m ; hanoi ( m, A , B , C ) ; 3.3.2 递归调用 例例3-193-19 第200页 第201页 3.4 函数指针 函数、应用程序是编译器处理的对象 每一个函数模块都有一个首地址,称为函数的入口地址, (函数指针) 函数调用:找到函数入口地址;传递参数 不带括号的函数名就是函数入口地址 第202页 3.4.1 函数的地址 int a 0x0065FDF4 0x00401014 内存分配 数据对象地址 代码对象地址 3.4.1 函数的地址 第203页 / 例3-20 函数和数据的测试 #include using namespace std ; void simple()/ 定义一个简单函数 cout using namespace std ; void simple()/ 定义一个简单函数 cout using namespace std ; void simple()/ 定义一个简单函数 cout using namespace std ; void simple()/ 定义一个简单函数 cout using namespace std ; void simple()/ 定义一个简单函数 cout using namespace std ; void simple()/ 定义一个简单函数 cout using namespace std ; void simple()/ 定义一个简单函数 cout using namespace std ; int sum ( int x , int y ) return x + y ; int product ( int x , int y ) return x * y ; int main() int ( * pf ) ( int , int ) ; int a , b , result ; cout a ; cout b ; pf = sum ; result = pf ( a , b ) ; cout using namespace std ; int sum ( int x , int y ) return x + y ; int product ( int x , int y ) return x * y ; int main() int ( * pf ) ( int , int ) ; int a , b , result ; cout a ; cout b ; pf = sum ; result = pf ( a , b ) ; cout using namespace std ; int sum ( int x , int y ) return x + y ; int product ( int x , int y ) return x * y ; int main() int ( * pf ) ( int , int ) ; int a , b , result ; cout a ; cout b ; pf = sumpf = sum ; result = pf ( a , b )pf ( a , b ) ; cout using namespace std ; int sum ( int x , int y ) return x + y ; int product ( int x , int y ) return x * y ; int main() int ( * pf ) ( int , int ) ; int a , b , result ; cout a ; cout b ; pf = sumpf = sum ; result = pf ( a , b )pf ( a , b ) ; cout using namespace std ; int sum ( int x , int y ) return x + y ;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 工会知识培训
- 腹腔镜子宫切除护理配合
- 九年级化学上册 第五单元 定量研究化学反应 第二节 化学反应的表示教学设计 鲁教版
- 餐巾折花培训方案
- 2024中国航天科工集团有限公司档案馆招聘3人笔试参考题库附带答案详解
- 六年级上册心理健康教育教案-5插上创造的翅膀|辽大版
- 船舶预防火灾培训
- 动火作业安全培训课件
- 人教版历史与社会八年级上册第二单元第一课《西欧封建国家与基督教文明》 教学设计1
- 双重预防体系练习试卷附答案(一)
- 中考数学专题复习《代数推理题》知识点梳理及典例讲解课件
- 抖音电商直播运营团队组织架构与职责说明
- 直流伺服电机控制系统设计
- 提水试验过程及数据处理
- 河道护坡工程安全管理体系与措施
- 《口腔基础医学概要》课件-口腔的功能
- SNT 2360.9-2009进出口食品添加剂检验规程第9部分:着色剂
- 资产分配方案
- 【中考物理】2023届北京市第二轮复习-科普阅读题(提升题)含解析
- dr钻戒的营销策划书
- 新人教版五年级小学数学全册奥数(含答案)
评论
0/150
提交评论