当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

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

资源类别:文库,文档格式:PDF,文档页数:92,文件大小:3.46MB,团购合买
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 预处理命令
点击下载完整版文档(PDF)

第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; }

点击下载完整版文档(PDF)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共92页,可试读20页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有