- 0
- 0
- 约2.83万字
- 约 112页
- 2021-09-29 发布于北京
- 举报
C++程序设计主讲 中南大学软件学院 谭长庚mailto:cgtan@163.comcgtan@163.com2011.10Chapter 5 Functions(函数)ObjectivesTo create functions, invoke functions, and pass arguments to a function (§5.2-5.4)(创建、调用函数,参数传递).To understand the differences between pass-by-value(值传递) and pass-by-reference (引用传递)(§5.5-5.6).To use function overloading(函数重载) and understand ambiguous overloading(理解重载歧义) (§5.7).To use function prototypes(函数原型) for declaring function headers To know how to use default arguments(默认/缺省参数) (§5.9).To create header files for reusing functions (§5.11).To determine the scope(作用域) of local and global variables To develop applications using the C++ mathematical functions To design and implement functions using stepwise refinement (逐步求精)(§5.15).To improve runtime efficiency using inline functions(内联函数) 5.1 Introducing Functionsfunction : a collection of statements that are grouped together to perform an operation(task)是结构化程序的主要构成单元,是面向对象程序设计中,对功能的抽象功能模块:求解较小问题的算法和程序。 各功能模块可以先单独设计,然后将求解所有子问题的模块组合成求解原问题的程序。逐步求精(stepwise refinement):复杂问题分解成子问题,逐步分解,直到能用单个函数解决小问题。方法:自顶向下、自底向上C++程序由一个或多个函数构成:有且仅有一个main()函数,若干其他函数;程序总是从main()函数开始执行库函数+用户自定义函数若无参数,写void是被初始化的内部变量,寿命和可见性仅限于函数内部函数返回值类型。若无返回值,写void(空类型)5.2 Defining Functions描述函数功能的代码称函数定义函数定义的语法形式类型函数名(形式参数表) { 语句序列}函数签名main(void)int max(int n, int m){ int result; if (nm) result=n; else result=m; return result;}//不能int max( int n, m) //不能int max(int n, int m);函数签名Function signature is the combination of the function name and the parameter list. The variables defined in the function header are known as formal parameters.(形式参数) When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument.(实际参数)int abs(int x){ int result=x; if (x0) result=-x; return result;}int main(void){ int y; ciny; coutabs(2)abs(y);}int abs(int x){ int result=x; if (x0) result=-x; return result;}函数返回值A Function may return a value. The returnValueType is the data type of the value the function
您可能关注的文档
最近下载
- 鲁科版小学英语四年级下册单词默写表.pdf VIP
- 16J914-1 公用建筑卫生间.docx VIP
- 2025-2026学年广东省深圳市宝安区八年级(上)期末历史试卷(含答案).docx
- 四川省加油(气)站安全生产标准化所需资料清单.pdf VIP
- 2026年浙江高考1月首考英语应用文课件.pptx
- 电子学会等级考试三级真题.docx VIP
- 2025年湖南大众传媒职业技术学院单招笔试综合素质试题库含答案解析.docx VIP
- 一种货叉矫正装置及方法.pdf VIP
- 2025年湖南大众传媒职业技术学院单招笔试综合素质试题库含答案解析.docx VIP
- 2025年度民主生活会领导干部个人发言提纲三篇.docx VIP
原创力文档

文档评论(0)