




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年三明永安市事业单位专门面向驻军随军家属公开招聘考前自测高频考点模拟试题参考答案详解
- 2025广西百色市那坡县百南乡招聘村级防贫监测员1人考前自测高频考点模拟试题带答案详解
- 2025年永新县面向社会公开招聘城市社区专职网格员【37人】模拟试卷及1套完整答案详解
- 2025年上海市疾病预防控制中心(上海市预防医学科学院)初级岗位公开招聘考前自测高频考点模拟试题(含答案详解)
- 2025年浙江杭州市时代小学招聘校医1人模拟试卷及完整答案详解
- 2025广西农业科学院甘蔗研究所甘蔗生物固氮团队公开招聘1人模拟试卷及答案详解(网校专用)
- 2025湖北武汉江夏区第一人民医院(协和江南医院)招聘35人考前自测高频考点模拟试题及答案详解(夺冠系列)
- 2025江苏省检察官学院招聘高层次人才1人考前自测高频考点模拟试题完整答案详解
- 2025福建三明永安市公安局招聘警务辅助人员19人模拟试卷及答案详解(全优)
- 2025年山东土地乡村振兴集团有限公司招聘考前自测高频考点模拟试题附答案详解(考试直接用)
- 山西中考语文5年(21-25)真题分类汇编-文学类文本阅读
- 2025云南红河红家众服经营管理有限公司社会招聘工作人员8人笔试模拟试题及答案解析
- 2025关于信息技术外包合同
- 行政法知识竞赛题及答案
- 河北省金太阳2025-2026学年高三上学期9月联考语文试卷
- 组织工程瓣膜修复研究-洞察及研究
- 自主可控人工智能智能决策系统研究报告
- 2.1《整十、整百数乘一位数的口算和估算》(课件) -2025-2026学年三年级数学上册 苏教版
- 2025年四川基层法律服务工作者执业核准考试综合试题及答案一
- 招商银行ai面试试题及答案
- Z20+名校联盟(浙江省名校新高考研究联盟)2026届高三第一次联考化学及答案
评论
0/150
提交评论