c++程序设计基础3-函数ppt课件_第1页
c++程序设计基础3-函数ppt课件_第2页
c++程序设计基础3-函数ppt课件_第3页
c++程序设计基础3-函数ppt课件_第4页
c++程序设计基础3-函数ppt课件_第5页
已阅读5页,还剩345页未读 继续免费阅读

下载本文档

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

文档简介

1、第第1 1页页第第2 2页页 3.1 3.1 函数的定义与调用函数的定义与调用 3.2 3.2 函数参数的传递函数参数的传递 3.3 3.3 函数调用机制函数调用机制 3.4 3.4 函数指针函数指针 3.5 3.5 内联函数和重载函数内联函数和重载函数 3.6 3.6 变量存储特性与标识符作用域变量存储特性与标识符作用域 3.7 3.7 多文件结构程序多文件结构程序 3.8 3.8 命名空间命名空间 3.9 终止程序执行终止程序执行 小结小结第第3 3页页 函数函数FunctionFunction是功能抽象的模块是功能抽象的模块函数函数参数参数返回值返回值输入流输入流输出流输出流第第4 4页

2、页 函数函数FunctionFunction是功能抽象的模块是功能抽象的模块 函数作用函数作用 任务划分;代码重用任务划分;代码重用 函数是函数是C+C+程序的重要组件程序的重要组件 第第5 5页页 函数定义由两部分组成:函数首部和函数操作描述函数定义由两部分组成:函数首部和函数操作描述 函数调用是通过表达式或语句激活并执行函数代码的过程函数调用是通过表达式或语句激活并执行函数代码的过程 / 求圆柱体体积求圆柱体体积#includeusing namespace std ;double volume ( double radius, double height ) return 3.14 *

3、radius * radius * height ; int main() double vol, r, h ; cin r h ; vol = volume ( r, h ) ; cout Volume = vol endl ;第第6 6页页 函数定义由两部分组成:函数首部和函数操作描述函数定义由两部分组成:函数首部和函数操作描述 函数调用是通过表达式或语句激活并执行函数代码的过程函数调用是通过表达式或语句激活并执行函数代码的过程 / 求圆柱体体积求圆柱体体积#includeusing namespace std ;double volume ( double radius, double

4、height ) return 3.14 * radius * radius * height ; int main() double vol, r, h ; cin r h ; vol = volume ( r, h ) ; cout Volume = vol endl ;函数定义函数定义第第7 7页页 函数定义由两部分组成:函数首部和函数操作描述函数定义由两部分组成:函数首部和函数操作描述 函数调用是通过表达式或语句激活并执行函数代码的过程函数调用是通过表达式或语句激活并执行函数代码的过程 / 求圆柱体体积求圆柱体体积#includeusing namespace std ;double

5、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 Volume = vol endl ;函数调用函数调用第第8 8页页函数定义形式函数定义形式 类型类型 函数名函数名 ( 形式参数表形式参数表 ) 语句序列语句序列 3.1.1 3.1.1 函数定义函数定义 3.1.1 3.1.1 函数定义函数定义第第9 9页页函数定义形式函数定义形式 类型类型 函

6、数名函数名 ( 形式参数表形式参数表 ) 语句序列语句序列 3.1.1 3.1.1 函数定义函数定义 函数头函数头函数接口,包括:函数接口,包括:3.1.1 3.1.1 函数定义函数定义第第1010页页函数定义形式函数定义形式 3.1.1 3.1.1 函数定义函数定义 函数头函数头函数接口,包括:函数接口,包括:函数返回值类型函数返回值类型 函数体中由函数体中由 return 语句返回的值的类型。没有语句返回的值的类型。没有 返回值其类型为返回值其类型为void3.1.1 3.1.1 函数定义函数定义第第1111页页函数定义形式函数定义形式 类型类型 函数名函数名 ( 形式参数表形式参数表 )

7、 语句序列语句序列 3.1.1 3.1.1 函数定义函数定义 函数头函数头函数接口,包括:函数接口,包括:函数返回值类型函数返回值类型 函数体中由函数体中由 return 语句返回的值的类型。没有语句返回的值的类型。没有 返回值其类型为返回值其类型为void函数名函数名 用户定义标识符用户定义标识符3.1.1 3.1.1 函数定义函数定义第第1212页页函数定义形式函数定义形式 类型类型 函数名函数名 ( 形式参数表形式参数表 ) 语句序列语句序列 3.1.1 3.1.1 函数定义函数定义 函数头函数头函数接口,包括:函数接口,包括:函数返回值类型函数返回值类型 函数体中由函数体中由 retu

8、rn 语句返回的值的类型。没有语句返回的值的类型。没有 返回值其类型为返回值其类型为void函数名函数名 用户定义标识符用户定义标识符形式参数表形式参数表 逗号分隔的参数说明表列,缺省形式参数时不逗号分隔的参数说明表列,缺省形式参数时不 能省略圆括号。一般形式为:能省略圆括号。一般形式为:类型类型 参数参数1 ,类型,类型 参数参数2 , ,类型,类型 参数参数n 3.1.1 3.1.1 函数定义函数定义第第1313页页函数定义形式函数定义形式 3.1.1 3.1.1 函数定义函数定义 函数头函数头函数接口函数接口类型类型 函数名函数名 ( 形式参数表形式参数表 ) 语句序列语句序列 3.1.

9、1 3.1.1 函数定义函数定义第第1414页页例例3-1 3.1.1 3.1.1 函数定义函数定义 void printmessage ( ) cout How do you do! endl ; 3.1.1 3.1.1 函数定义函数定义第第1515页页例例3-13.1.1 3.1.1 函数定义函数定义 void printmessage ( ) cout How do you do! endl ; 函数返回值类型函数返回值类型无返回值无返回值3.1.1 3.1.1 函数定义函数定义第第1616页页例例3-13.1.1 3.1.1 函数定义函数定义 void printmessage ( )

10、 cout How do you do! endl ; 函数名函数名3.1.1 3.1.1 函数定义函数定义第第1717页页例例3-13.1.1 3.1.1 函数定义函数定义 void printmessage ( ) cout How do you do! endl ; 形式参数表形式参数表无参数无参数3.1.1 3.1.1 函数定义函数定义第第1818页页例例3-13.1.1 3.1.1 函数定义函数定义 void printmessage ( ) cout How do you do! y ) return x ; else return y ; 3.1.1 3.1.1 函数定义函数定义

11、第第2020页页例例3-23.1.1 3.1.1 函数定义函数定义 double max ( double x , double y ) if ( x y ) return x ; else return y ; 函数返回值类型函数返回值类型3.1.1 3.1.1 函数定义函数定义第第2121页页例例3-23.1.1 3.1.1 函数定义函数定义 double max ( double x , double y ) if ( x y ) return x ; else return y ; 函数名函数名3.1.1 3.1.1 函数定义函数定义第第2222页页例例3-23.1.1 3.1.1 函

12、数定义函数定义 double max ( double x , double y ) if ( x y ) return x ; else return y ; 形式参数表形式参数表3.1.1 3.1.1 函数定义函数定义第第2323页页例例3-23.1.1 3.1.1 函数定义函数定义 double max ( double x , double y ) if ( x y ) return x ; else return y ; 函数体函数体3.1.1 3.1.1 函数定义函数定义第第2424页页例例3-23.1.1 3.1.1 函数定义函数定义 double max ( double x

13、, double y ) if ( x y ) return x ; else return y ; 返回值返回值return return 语句形式:语句形式:return return 表达式表达式 或或return return (表达式(表达式 )作用:作用: 返回函数值返回函数值 不再执行后续语句,程序控制返回调用点不再执行后续语句,程序控制返回调用点 一个函数体内可以有多个一个函数体内可以有多个return return 语句语句 表达式表达式 返回值的类型与函数类型不相同时,返回值的类型与函数类型不相同时, 自动强制转换成函数的类型自动强制转换成函数的类型3.1.1 3.1.1

14、函数定义函数定义第第2525页页例例3-23.1.1 3.1.1 函数定义函数定义 double max ( double x , double y ) if ( x y ) return x ; else return y ; 3.1.1 3.1.1 函数定义函数定义第第2626页页调用形式调用形式 函数名函数名 ( 实际参数表实际参数表 )3.1.2 3.1.2 函数调用函数调用3.1.2 3.1.2 函数调用函数调用第第2727页页调用形式调用形式 3.1.2 3.1.2 函数调用函数调用3.1.2 3.1.2 函数调用函数调用第第2828页页调用形式调用形式 函数名函数名 ( 实际参数

15、表实际参数表 )3.1.2 3.1.2 函数调用函数调用函数名函数名 函数的入口地址函数的入口地址3.1.2 3.1.2 函数调用函数调用第第2929页页调用形式调用形式 函数名函数名 ( 实际参数表实际参数表 )3.1.2 3.1.2 函数调用函数调用函数名函数名 函数的入口地址函数的入口地址实际参数表实际参数表 与形式参数必须在个数、类型、位置一一对应与形式参数必须在个数、类型、位置一一对应3.1.2 3.1.2 函数调用函数调用第第3030页页3.1.2 3.1.2 函数调用函数调用#includeusing namespace std ;void printmessage () cou

16、t How do you do! endl ; int main() printmessage() ; 例例3-13.1.2 3.1.2 函数调用函数调用第第3131页页3.1.2 3.1.2 函数调用函数调用#includeusing namespace std ;void printmessage () cout How do you do! endl ; int main() printmessage() ; 函数调用语句函数调用语句例例3-13.1.2 3.1.2 函数调用函数调用第第3232页页3.1.2 3.1.2 函数调用函数调用例例3-2#includeusing namesp

17、ace 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 max( m, 3.5 ) endl ; 3.1.2 3.1.2 函数调用函数调用第第3333页页3.1.2 3.1.2 函数调用函数调用例例3-2#includeusing namespace std ;double max ( double x , double y ) if ( x y ) return

18、 x ; else return y ; int main() double a, b; cin a b ; double m = max( a, b ); cout max( m, 3.5 ) endl ; 函数调用表达式函数调用表达式3.1.2 3.1.2 函数调用函数调用第第3434页页3.1.2 3.1.2 函数调用函数调用例例3-2#includeusing namespace std ;double max ( double x , double y ) if ( x y ) return x ; else return y ; int main() double a, b; ci

19、n a b ; double m = max( a, b ); cout max( m, a+3.5 ) endl ; cout max( max( a, b ) , a+3.5 ) endl ;实际参数是表达式实际参数是表达式3.1.2 3.1.2 函数调用函数调用第第3535页页3.1.3 3.1.3 函数原型函数原型 函数原型的作用是告诉编译器有关函数的信息:函数原型的作用是告诉编译器有关函数的信息:函数的名字函数的名字函数返回的数据类型函数返回的数据类型函数要接受的参数个数、参数类型和参数的顺序函数要接受的参数个数、参数类型和参数的顺序 编译器根据函数原型检查函数调用的正确性编译器根据

20、函数原型检查函数调用的正确性 函数原型的形式:函数原型的形式:类型类型 函数名函数名 ( 形式参数表形式参数表 );3.1.3 3.1.3 函数原型函数原型第第3636页页3.1.3 3.1.3 函数原型函数原型 函数原型的作用是告诉编译器有关函数的信息:函数原型的作用是告诉编译器有关函数的信息:函数的名字函数的名字函数返回的数据类型函数返回的数据类型函数要接受的参数个数、参数类型和参数的顺序函数要接受的参数个数、参数类型和参数的顺序 编译器根据函数原型检查函数调用的正确性编译器根据函数原型检查函数调用的正确性 函数原型的形式:函数原型的形式:类型类型 函数名函数名 ( 形式参数表形式参数表

21、);函数原型是函数原型是声明语句声明语句3.1.3 3.1.3 函数原型函数原型第第3737页页3.1.3 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 Maximum = m2 y ) return x ; else return y ; 使用函数原型使用函数原型3.1

22、.3 3.1.3 函数原型函数原型第第3838页页3.1.3 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 Maximum = m2 y ) return x ; else return y ; 函数原型的参数表函数原型的参数表不需要参数名不需要参数名使用函数原型使用函数原型3.1.3

23、3.1.3 函数原型函数原型第第3939页页3.1.3 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 Maximum = m2 y ) return x ; else return y ; 函数调用出现在定义之前函数调用出现在定义之前函数原型声明是必须的函数原型声明是必须的使用函数原型使

24、用函数原型3.1.3 3.1.3 函数原型函数原型第第4040页页3.1.3 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 Maximum = m2 en

25、dl ;3.1.3 3.1.3 函数原型函数原型第第4141页页3.1.3 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 Maximum = m2 endl ;函数定义出

26、现在调用之前函数定义出现在调用之前无必要作函数原型声明无必要作函数原型声明3.1.3 3.1.3 函数原型函数原型第第4242页页3.1.3 3.1.3 函数原型函数原型函数原型函数原型说明说明int abs( int n );n的绝对值的绝对值double cos( double x );x(弧度)的余弦(弧度)的余弦double exp( double x );指数函数,指数函数,exdouble fabs( double x );x的绝对值的绝对值double fmod( double x, double y );x/y的浮点余数的浮点余数double log( double x );x

27、的自然对数(以的自然对数(以e为底)为底)double log10( double x );x的对数(以的对数(以10为底)为底)double pow( double x, double y );求幂,求幂,xydouble sin( double x );x(弧度)的正弦(弧度)的正弦double sqrt( double x );x的平方根的平方根double tan( double x );x(弧度)的正切(弧度)的正切 cmath中几个常用的数学函数中几个常用的数学函数 3.1.3 3.1.3 函数原型函数原型第第4343页页3.1.3 3.1.3 函数原型函数原型/ 例例3-3 用库

28、函数求正弦和余弦值用库函数求正弦和余弦值#include #include using namespace std ;int main() double PI = 3.1415926535; double x, y; x = PI/ 2; y = sin( x ); cout sin( x ) = y endl ; y = cos( x ); cout cos( x ) = y endl ;3.1.3 3.1.3 函数原型函数原型第第4444页页3.1.3 3.1.3 函数原型函数原型/ 例例3-3 用库函数求正弦和余弦值用库函数求正弦和余弦值#include #include using n

29、amespace std ;int main() double PI = 3.1415926535; double x, y; x = PI / 2; y = sin( x ); cout sin( x ) = y endl ; y = cos( x ); cout cos( x ) = y endl ;包含头文件包含头文件3.1.3 3.1.3 函数原型函数原型第第4545页页3.1.3 3.1.3 函数原型函数原型/ 例例3-3 用库函数求正弦和余弦值用库函数求正弦和余弦值#include #include using namespace std ;int main() double PI

30、 = 3.1415926535; double x, y; x = PI / 2; y = sin( x ); cout sin( x ) = y endl ; y = cos( x ); cout cos( x ) = y endl ;调用库函数调用库函数3.1.3 3.1.3 函数原型函数原型第第4646页页第第4747页页C+有三种参数传递机制:有三种参数传递机制:值传递值传递指针传递指针传递引用传递引用传递 3.2 函数参数的传递第第4848页页3.2.13.2.1 传值参数传值参数 调用函数时,实参表达式的值被复制到相应形参标识的对象中,调用函数时,实参表达式的值被复制到相应形参标识

31、的对象中, 并按形参类型强制转换并按形参类型强制转换 函数内对形参的访问、修改,都在形参的标识对象进行函数内对形参的访问、修改,都在形参的标识对象进行 函数返回时,形参对象被撤消,不影响实参的值函数返回时,形参对象被撤消,不影响实参的值 值传送的实参可以是常量、有确定值的变量或表达式值传送的实参可以是常量、有确定值的变量或表达式 函数返回值通过匿名对象传递函数返回值通过匿名对象传递 3.2.1 传值参数第第4949页页/ 例例3-4 强制类型转换强制类型转换#includeusing namespace std ;int main ( ) double add1 ( double , doub

32、le ) ; / 函数原型函数原型 double add2 ( int , int ) ; / 函数原型函数原型 double a , b, c ; cin a b ; c = add1 ( a , b ) ; cout c1= c endl ; c = add2 ( 1/a , 1/b) ; cout c2= c endl ;double add1 ( double x , double y ) return ( x + y ) ; double add2 ( int i , int j ) return ( i + j ) ; 3.2.1 传值参数第第5050页页/ 例例3-4 强制类型转

33、换强制类型转换#includeusing namespace std ;int main ( ) double add1 ( double , double ) ; / 函数原型函数原型 double add2 ( int , int ) ; / 函数原型函数原型 double a , b, c ; cin a b ; c = add1 ( a , b ) ; cout c1= c endl ; c = add2 ( 1/a , 1/b) ; cout c2= c endl ;double add1 ( double x , double y ) return ( x + y ) ; doub

34、le add2 ( int i , int j ) return ( i + j ) ; 类型强制转换类型强制转换截取整数部分传送给形参截取整数部分传送给形参3.2.1 传值参数第第5151页页#includeusing namespace std ;int add(int , int ) ;int main() int a, b, c ; cin a b; c = add(a,b) ; cout c = c endl ;int add(int i, int j ) i + + ; j + + ; return ( i + j ); / 例例3-5 值参传递值参传递3.2.1 传值参数第第52

35、52页页#includeusing namespace std ;int add(int , int ) ;int main() int a, b, c ; cin a b; c = add(a,b) ; cout c = c endl ;int add(int i, int j ) i + + ; j + + ; return ( i + j ); abc int a, b, c ;/ 例例3-5 值参传递值参传递3.2.1 传值参数第第5353页页#includeusing namespace std ;int add(int , int ) ;int main() int a, b, c

36、 ; cin a b; c = add(a,b) ; cout c = c a b;/ 例例3-5 值参传递值参传递3.2.1 传值参数第第5454页页#includeusing namespace std ;int add(int , int ) ;int main() int a, b, c ; cin a b; c = add(a,b) ; cout c = c endl ;int add(int i, int j ) i + + ; j + + ; return ( i + j ); abc24 c = add(a,b) ;/ 例例3-5 值参传递值参传递3.2.1 传值参数第第555

37、5页页#includeusing namespace std ;int add(int , int ) ;int main() int a, b, c ; cin a b; c = add(a,b) ; cout c = c endl ;int add(int i, int j ) i + + ; j + + ; return ( i + j ); abcij2424 int add(int i, int j )/ 例例3-5 值参传递值参传递3.2.1 传值参数第第5656页页24ij#includeusing namespace std ;int add(int , int ) ;int

38、main() int a, b, c ; cin a b; c = add(a,b) ; cout c = c endl ;int add(int i, int j ) i + + ; j + + ; return ( i + j ); abc2424 i + + ; j + + ;/ 例例3-5 值参传递值参传递3.2.1 传值参数第第5757页页#includeusing namespace std ;int add(int , int ) ;int main() int a, b, c ; cin a b; c = add(a,b) ; cout c = c endl ;int add(

39、int i, int j ) i + + ; j + + ; return ( i + j ); abcij2424obj return ( i + j );/ 例例3-5 值参传递值参传递3 + 53.2.1 传值参数第第5858页页abcij2424obj8/ 例例3-5 值参传递值参传递3.2.1 传值参数#includeusing namespace std ;int add(int , int ) ;int main() int a, b, c ; cin a b; c = add(a,b) ; cout c = c endl ;int add(int i, int j ) i +

40、+ ; j + + ; return ( i + j ); return ( i + j );第第5959页页#includeusing namespace std ;int add(int , int ) ;int main() int a, b, c ; cin a b; c = add(a,b) ; cout c = c endl ;int add(int i, int j ) i + + ; j + + ; return ( i + j ); abcij24824 c = add(a,b) ;/ 例例3-5 值参传递值参传递obj83.2.1 传值参数第第6060页页abc248/ 例

41、例3-5 值参传递值参传递3.2.1 传值参数#includeusing namespace std ;int add(int , int ) ;int main() int a, b, c ; cin a b; c = add(a,b) ; cout c = c endl ;int add(int i, int j ) i + + ; j + + ; return ( i + j ); c = add(a,b) ;第第6161页页#includeusing namespace std ;int add(int , int ) ;int main() int a, b, c ; cin a b

42、; c = add(a,b) ; cout c = c endl ;int add(int i, int j ) i + + ; j + + ; return ( i + j ); abc248输出输出c = 8 cout c = c endl ;/ 例例3-5 值参传递值参传递3.2.1 传值参数第第6262页页例例3-6 计算圆筒的体积计算圆筒的体积分析: 分别定义函数求不同几何体的体积 圆柱体的体积 = r2hdouble CylinderVolume(double r, double h ) ; 圆筒的体积 = 外圆柱体的体积 内圆柱体的体积 double DonutSize(doub

43、le Outer, double Inner, double Height) ;3.2.1 传值参数第第6363页页/ 通过参数半径通过参数半径 r 和高度和高度 h,返回圆柱体体积,返回圆柱体体积double CylinderVolume( double r, double h ) const double PI = 3.1415; return PI * r * r * h;/ 通过参数外圆柱体半径通过参数外圆柱体半径 Outer,内圆柱体半径,内圆柱体半径 Inner / 和圆柱体高度和圆柱体高度 Height 返回圆筒体积返回圆筒体积double DonutSize(double Ou

44、ter, double Inner, double Height) double OuterSize = CylinderVolume(Outer, Height); double HoleSize = CylinderVolume(Inner, Height); return OuterSize - HoleSize;3.2.1 传值参数例例3-6 计算圆筒的体积计算圆筒的体积第第6464页页/ 通过参数半径通过参数半径 r 和高度和高度 h,返回圆柱体体积,返回圆柱体体积double CylinderVolume( double r, double h ) const double PI

45、= 3.1415; return PI * r * r * h;/ 通过参数外圆柱体半径通过参数外圆柱体半径 Outer,内圆柱体半径,内圆柱体半径 Inner / 和圆柱体高度和圆柱体高度 Height 返回圆筒体积返回圆筒体积double DonutSize(double Outer, double t Inner, double Height) double OuterSize = CylinderVolume(Outer, Height); double HoleSize = CylinderVolume(Inner, Height); return OuterSize - HoleS

46、ize;3.2.1 传值参数例例3-6 计算圆筒的体积计算圆筒的体积第第6565页页#includeusing 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 size of the donu

47、t is : DonutSize( OuterRadius, InnerRadius, Height) endl ;double CylinderVolume( double r, double h ) const double PI = 3.1415; return PI * r * r * h;double DonutSize(double Outer, double Inner, double Height) double OuterSize = CylinderVolume(Outer, Height); double HoleSize = CylinderVolume(Inner,

48、Height); return OuterSize - HoleSize;3.2.1 传值参数例例3-6 计算圆筒的体积计算圆筒的体积第第6666页页#includeusing 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 ; co

49、ut Height ; cout size of the donut is : DonutSize( OuterRadius, InnerRadius, Height) endl ;double CylinderVolume( double r, double h ) const double PI = 3.1415; return PI * r * r * h;double DonutSize(double Outer, double Inner, double Height) double OuterSize = CylinderVolume(Outer, Height); double

50、HoleSize = CylinderVolume(Inner, Height); return OuterSize - HoleSize;函数原型函数原型3.2.1 传值参数例例3-6 计算圆筒的体积计算圆筒的体积第第6767页页#includeusing namespace std ;double CylinderVolume( double r, double h );double DonutSize(double Outer, double Inner, double Height);int main() double OuterRadius, InnerRadius, Height;

51、 cout OuterRadius ; cout InnerRadius ; cout Height ; cout size of the donut is : DonutSize( OuterRadius, InnerRadius, Height) endl ;double CylinderVolume( double r, double h ) const double PI = 3.1415; return PI* r * r * h;double DonutSize(double Outer, double Inner, double Height) double OuterSize

52、= CylinderVolume(Outer, Height); double HoleSize = CylinderVolume(Inner, Height); return OuterSize - HoleSize;函数定义函数定义3.2.1 传值参数例例3-6 计算圆筒的体积计算圆筒的体积第第6868页页#includeusing namespace std ;double CylinderVolume( double r, double h );double DonutSize(double Outer, double Inner, double Height);int main()

53、double OuterRadius, InnerRadius, Height; cout OuterRadius ; cout InnerRadius ; cout Height ; cout size of the donut is : DonutSize( OuterRadius, InnerRadius, Height) endl ;double CylinderVolume( double r, double h ) const double PI = 3.1415; return PI * r * r * h;double DonutSize(double Outer, doubl

54、e Inner, double Height) double OuterSize = CylinderVolume(Outer, Height); double HoleSize = CylinderVolume(Inner, Height); return OuterSize - HoleSize;调用函数调用函数3.2.1 传值参数例例3-6 计算圆筒的体积计算圆筒的体积第第6969页页#includeusing namespace std ;double CylinderVolume( double r, double h );double DonutSize(double Outer,

55、 double Inner, double Height);int main() double OuterRadius, InnerRadius, Height; cout OuterRadius ; cout InnerRadius ; cout Height ; cout size of the donut is : DonutSize( OuterRadius, InnerRadius, Height) endl ;double CylinderVolume( double r, double h ) const double PI = 3.1415; return PI * r * r

56、 * h;double DonutSize(double Outer, double Inner, double Height) double OuterSize = CylinderVolume(Outer, Height); double HoleSize = CylinderVolume(Inner, Height); return OuterSize - HoleSize;3.2.1 传值参数例例3-6 计算圆筒的体积计算圆筒的体积第第7070页页 C+没有规定在函数调用时实际参数的求值顺序 若实际参数表达式之间有求值关联,同一个程序在不同编译器可能 产生不同的运行结果3.2.1 传值

57、参数第第7171页页#includeusing 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 5 + 11 = z ?!n ;3.2.1 传值参数第第7272页页#includeusing namespace std ;int add ( int x , int y ) return x + y ; int main ( ) int x = 4 , y = 6 ; int z = add ( +

58、 x , x + y ) ; cout 5 + 11 = z ?!n ;xyz46xy103.2.1 传值参数第第7373页页46xyz56xy105#includeusing 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 5 + 11 = z ?!n ;3.2.1 传值参数第第7474页页#includeusing namespace std ;int add ( int x , int y

59、) return x + y ; int main ( ) int x = 4 , y = 6 ; int z = add ( + x , x + y ) ; cout 5 + 11 = z ?!n ;xyz565xy10obj5 + 103.2.1 传值参数第第7575页页#includeusing 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 5 + 11 = z ?!n ;xyz565xy

60、105 + 10obj153.2.1 传值参数第第7676页页#includeusing 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 5 + 11 = z ?!n ;xyz565xy10obj153.2.1 传值参数第第7777页页#includeusing namespace std ;int add ( int x , int y ) return x + y ; int main ( )

温馨提示

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

评论

0/150

提交评论