川北医学院:《C++程序设计》课程教学资源(课件讲稿)第4章 函数与预处理

第4章函数与预处理 4.1概述 4.2 定义函数的一般形式 4.3 函数参数和函数的值 4.4函数的调用 *4.5 内置函数 *4.6 函数的重载 *4.7 函数模板 *4.8 看款认参数的函数 4.9函数的嵌套调用 4.10函数的递归调用 4.11局部变量和全高变量 4.12 变量的存储类别 4.13 变量属性小结 4.14 笑于变量的声明和定义 4.15 丙部函数和外部函数 4.16 预处理命令 017年4月26日12时17分 HOME 第4章函数与预处理 2 BACK NEXT
HOME2017年4月26日12时17分 第4章 函数与预处理 2 4.1 概述 4.2 定义函数的一般形式 4.3 函数参数和函数的值 4.4 函数的调用 *4.5 内置函数 *4.6 函数的重载 *4.7 函数模板 *4.8 有默认参数的函数 4.9 函数的嵌套调用 4.10 函数的递归调用 4.11 局部变量和全局变量 4.12 变量的存储类别 4.13 变量属性小结 4.14 关于变量的声明和定义 4.15 内部函数和外部函数 4.16 预处理命令

runctlon rreprocessor directives Main Content Function's definition,declaration and call Parameters transfer between functions Function's overloading Function template Functions with default format parameters Function's Nesting call Function's Recursion call 2017年4月26日12时17分 3 HOM正 第4章函数与预处理 BACK NEXT
HOME2017年4月26日12时17分 第4章 函数与预处理 3 Main Content • Function’s definition, declaration and call • Parameters transfer between functions • Function’s overloading • Function template • Functions with default format parameters • Function’s Nesting call • Function’s Recursion call

函数(function)概述 C++的函数(function)是完成既定任务的功能(过 程)体,它涵盖了数学函数和一般过程.所以基于 过程编程本质上就是基于函数编程。 为了便于规划、组织、编程和调试,一 般的做法是 把一个大的程序划分为若干个程序模块(即程序文件), 每一个模块实现一部分功能。 在程序进行编译时,以程序模块为编译单位,即分 别对每一个编译单位进行编译。如果发现错误,可 以在本程序模块范围内查错并改正 2017年4月26日12时17分 第4章函数与预处理 BAC
HOME2017年4月26日12时17分 第4章 函数与预处理 4 C++的函数(function)是完成既定任务的功能(过 程)体,它涵盖了数学函数和一般过程.所以基于 过程编程本质上就是基于函数编程。 为了便于规划、组织、编程和调试,一般的做法是 把一个大的程序划分为若干个程序模块(即程序文件), 每一个模块实现一部分功能。 在程序进行编译时,以程序模块为编译单位,即分 别对每一个编译单位进行编译。如果发现错误,可 以在本程序模块范围内查错并改正

1 Function's Definition Function is the basic abstract module in OOP,and it also is abstract for some tasks. Syntax Form of function definition: 类型标识符 函数名(形式参数表) 若无参数,写void 语句序列 是被初始化的内部 变量,寿命和可见 性仅限于函数内部 若无返回值,写void 017年4月26日12时17分 第4章函数与预处理 5 HOME BACK NEX
HOME2017年4月26日12时17分 第4章 函数与预处理 5 • Function is the basic abstract module in OOP, and it also is abstract for some tasks. • Syntax Form of function definition: 类型标识符 函数名(形式参数表) { 语句序列 } 若无参数,写void 是被初始化的内部 变量,寿命和可见 性仅限于函数内部 若无返回值,写void

Format parameter list name1,name2,... namen Return value of function Given by return sentence.For example: 。return0; 。return 4(1); If the function has no return value,which means void type,the return sentence can be omitted. 2017年4月26日12时17分 HOM正 第4章函数与预处理 6 BACK NEXT
HOME2017年4月26日12时17分 第4章 函数与预处理 6 • Format parameter list name1 , name2 , ..., namen • Return value of function – Given by return sentence. For example: • return 0; • return (1); – If the function has no return value, which means void type,the return sentence can be omitted

2 Function Declaration Declaration principle: If a function is defined before its being used,the declaration may be omitted; If a function is used before its definition,it must be declared in advanced. Declaration method: Return type Name parameters list ) For example: int sum(int n,float m); 函数申明可以不包含参数的名字,而只要包含函数的类型 017年4月26日12时17分 HOME 第4章函数与预处理 BACK NEX
HOME2017年4月26日12时17分 第4章 函数与预处理 7 • Declaration principle: If a function is defined before its being used, the declaration may be omitted; If a function is used before its definition, it must be declared in advanced. • Declaration method: • Return type Name (parameters list ); For example: int sum(int n, float m); //函数申明可以不包含参数的名字,而只要包含函数的类型

3 Function's type Getting parameters and returning value.For example: int bigger(int a,int b) { return(a>b)?a:b; Getting parameters but not return value.For example: void delay(long a) for(int i=1;i<=a;i++); C++要求在定义函数时必须指定函数的类型。 017年4月26日12时17分 HOME 第4章函数与预处理 8 BACK NEXT
HOME2017年4月26日12时17分 第4章 函数与预处理 8 • Getting parameters and returning value. For example: int bigger(int a, int b) { return(a>b)?a:b; } • Getting parameters but not return value. For example: void delay(long a) { for(int i=1;i<=a;i++); } • C++要求在定义函数时必须指定函数的类型

3 Function's type Getting no parameters but returning value.For example: int geti() { int x; cout>X; return X; } Getting no parameters and returning no value.For example: void message() cout<<“This is a message.ln” } 017年4月26日12时17分 HOME 第4章函数与预处理 9 BACK NEXT
HOME2017年4月26日12时17分 第4章 函数与预处理 9 • Getting no parameters but returning value. For example: int geti() { int x; cout>x; return x; } • Getting no parameters and returning no value. For example: void message() { cout<<“This is a message.\n” }

4 Function's Call Declare a function prototype at the beginning of the program before it being called: 类型标识符被调用函数名(含类型说明的形参表); Call Form 函数名(实参列表) max(a,b) Nesting call Nesting definition is not allowed in any function, but nesting call is permitted. Recursion call Function can call itself directly or indirectly. 0了年4月26日12时17分 第4章函数与预处理 10 BACK NEXT
HOME2017年4月26日12时17分 第4章 函数与预处理 10 • Declare a function prototype at the beginning of the program before it being called: 类型标识符 被调用函数名 (含类型说明的形参表); • Call Form 函数名(实参列表) max(a,b) • Nesting call Nesting definition is not allowed in any function, but nesting call is permitted. • Recursion call Function can call itself directly or indirectly

例4.1在主函数中调用其他函数 #include using namespace std; void printstar(void) l定义printstari函数 { C0UtK<"*********"<<edl;/输出30个 } void print_message(void) l定义print_message函数 { cout<<"Welcome to C++!"<<endl; 输出一行文字 int main(void) printstar() ∥调用printstar函数 print_message()月 i调用print_message函数 printstar()月 ∥调用printstar函数 return 0; 2017年4月26日12时17分 HOM正 第4章函数与预处理 11 BACK NEXT
HOME2017年4月26日12时17分 第4章 函数与预处理 11 #include using namespace std; void printstar(void) //定义printstar函数 { cout<<"****************************** "<<endl; //输出30个“*” } void print_message(void) //定义print_message函数 { cout<<"Welcome to C++!"<<endl; //输出一行文字 } int main(void) { printstar( ); //调用printstar 函数 print_message( ); //调用print_message函数 printstar( ); //调用printstar 函数 return 0; }
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第3章 程序设计初步.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第2章 数据类型与表达式 Data Types & Expression.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第1章 C++的初步知识(主讲:祝元仲)C++ Programming.pdf
- 同济大学:《Visual Basic程序设计简明教程》配套PPT课件讲稿(第三版)第10章 数据库应用基础.ppt
- 同济大学:《Visual Basic程序设计简明教程》配套PPT课件讲稿(第三版)第7章 用户界面设计、第8章 数据文件、第9章 图形操作.ppt
- 同济大学:《Visual Basic程序设计简明教程》配套PPT课件讲稿(第三版)第4章 VB控制结构、第5章 数组和自定义类型、第6章 过程.ppt
- 同济大学:《Visual Basic程序设计简明教程》配套PPT课件讲稿(第三版)第1章 Visual Basic程序设计概述、第2章 VB可视化编程基础、第3章 VB语言基础.ppt
- 甘肃农业大学:《VB程序设计基础》课程教学资源(PPT讲稿)C语言概述.ppt
- 甘肃农业大学:《VB程序设计基础》课程教学资源(作业习题)VB习题1.pdf
- 甘肃农业大学:《VB程序设计基础》课程教学资源(授课教案)第一章 VB概述.doc
- 甘肃农业大学:《VB程序设计基础》课程教学资源(教学大纲)Programming of Visual Basic.pdf
- Connectivity Analysis in Wireless Networks with Correlated Mobility and Cluster Scalability.pdf
- Asymptotic Analysis on Content Placement and Retrieval in MANETs.pdf
- Are we connected? Optimal Determination of Source-destination Connectivity in Random Networks.pdf
- Mobility Weakens the Distinction between Multicast and Unicast.pptx
- Mobility Weakens the Distinction between Multicast and Unicast.pdf
- Impact of Social Relation and Group Size in Multicast Ad Hoc Networks.pptx
- Impact of Social Relation and Group Size in Multicast Ad Hoc Networks.pdf
- Optimal Secrecy Capacity-Delay Tradeoff in Large-Scale Mobile Ad Hoc Networks.pdf
- Two-Dimensional Route Switching in Cognitive Radio Networks:A Game-Theoretical Framework.ppt
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第5章 数组 Arrays.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第6章 指针.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第8章 类和对象.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第10章 运算符重载.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第11章 继承与派生.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第12章 多态性与虚函数 Polymorphism & Virtual Functions.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第9章 关于类和对象的进一步讨论.pdf
- 川北医学院:《单片机原理》课程教学资源(教学大纲)单片机原理与应用技术 Monolithic principle and application technology.pdf
- 川北医学院:《单片机原理》课程教学资源(考试大纲).pdf
- 川北医学院:《单片机原理》课程教学资源(教案)生物医学工程专业.pdf
- 川北医学院:《单片机原理》课程教学资源(讲稿,共八章)生物医学工程专业.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第1章 基础知识.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第2章 单片机的结构原理与简单应用.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第3章 8051指令系统.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第4章 汇编语言程序设计.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第5章 中断系统.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第6章 定时器/计数器.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第7章 并行扩展技术.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第8章 串行通信.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第9章 串行扩展技术.pdf