




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Chapter 6Modularity Using Functions: Part IFunction and Parameter DeclarationsA function that is called into action by its reference in another function is a called function(被调用函数被调用函数:一个函数通过被其他函数引用而起作用一个函数通过被其他函数引用而起作用)A function that calls another function is referred to as the calling function(调用
2、函数调用函数)Function and Parameter Declarations调用和传递数据到函数调用和传递数据到函数Function and Parameter DeclarationsFunction Prototypes(函数原型函数原型)The declaration statement for a function is referred to as a function prototype (一个函数的声明语句称为函数原型一个函数的声明语句称为函数原型) Declares the data type of the value that will be directly ret
3、urned by the function(函数原型声明将被这个函数直接返回的数值的数函数原型声明将被这个函数直接返回的数值的数据类型据类型) Declares the data type of the values that need to be transmitted to the called function when it is invoked(在它被调用时需要传递给被在它被调用时需要传递给被调用函数的数值的数据类型调用函数的数值的数据类型) returnDataType functionName (argument data types);Calling a FunctionArg
4、uments(参数参数): items enclosed in parentheses in a function call statement(函数调用语句中被圆括号附上的条目)(函数调用语句中被圆括号附上的条目) Other terms used for arguments are actual arguments(实际参数实际参数)and actual parameters(实参实参)Pass by value(按值传递按值传递): when a function receives copies of the values in each argument and must determ
5、ine where to store them before it does anything else(函数只是接受这些变量的数值复制品,并且在它做其他任函数只是接受这些变量的数值复制品,并且在它做其他任何事情之前必须确定在哪里存储这些数值。何事情之前必须确定在哪里存储这些数值。) Also referred to as call by value(数值调用数值调用)Calling a FunctionCalling a FunctionFunction Header LineFunction header(函数首部函数首部) : identifies the data type of th
6、e return value, provides the function with a name, and specifies the number, order, and type of values expected by the functionFunction body(函数体函数体): operates on the passed data and returns, at most, one valueThe argument names in the header line are known as parameters(参参数数)or formal parameters(形参形
7、参)and formal arguments(形式参数形式参数)Function Header LineFunction Header LineSome programmers prefer to put all called functions at the top of a program and make main() the last function listedEach C function is a separate and independent entity with its own parameters and variables(每个每个C 语言的函数都是分开的和独立的,
8、有它语言的函数都是分开的和独立的,有它们自己的参数和变量。们自己的参数和变量。) Nested functions are not permitted(决不允许把一个函数嵌套在另一决不允许把一个函数嵌套在另一个函数里面。个函数里面。 )Function Header LineEnds with a semicolonDoes not end with a semicolonFunction Header LineBasic (good) programming structure:preprocessor directivessymbolic constantsfunction prototy
9、pes can be placed hereint main() function prototypes can be placed here variable declarations; other executable statements; return value;Placement of Statements(语句的放置语句的放置)Returning a ValueFrom its side of the return transaction, the called function must provide: Data type of the returned value, whi
10、ch is specified in the functions header line Actual value being returned, which is specified by a return statementReturning a ValueReturning a ValueTo return a value, use a return statement return (expression); /or, return expression; The expression is evaluated first; its value is then automaticall
11、y converted to the return values data type as specified in the functions header line before being sent back to the calling function (表达式首先被计算。然后,表达式的数值在被送回到调用函数之前表达式首先被计算。然后,表达式的数值在被送回到调用函数之前自动地转换为在函数首部行中定义的返回值的数据类型。自动地转换为在函数首部行中定义的返回值的数据类型。)Returning a ValueFailure to exactly match the return value
12、 with the functions declared data type can lead to undesired results Return value is converted to the data type declared in the functions header line(返回的数值总是被转换为函数首部行中声明的数据类(返回的数值总是被转换为函数首部行中声明的数据类型)型)Returning a ValueStandard Library FunctionsThe standard library consists of 15 header filesBefore u
13、sing these functions, you must know The name of each available function The arguments required by each function The data type of the result (if any) returned by each function A description of what each function does How to include the library containing the desired functionT#include Mathematical Lib
14、rary FunctionsMathematical Library FunctionsThe rand() FunctionsRandom numbers(随机数随机数)are a series of numbers whose order cannot be predicted(次序不能被预知的一系列数字)(次序不能被预知的一系列数字)All C compilers provide a function for creating random numbers: rand() ,defined in the stdlib.h header file rand() produces rando
15、m numbers in the range 0 rand() RAND_MAX(RAND_MAX是一个取决于编译器的在头文件中被定义的符号常量。)是一个取决于编译器的在头文件中被定义的符号常量。)Scaling(比例缩放比例缩放)The method for adjusting the random numbers produced by a random-number generator to reside within a specified range is called scaling(调调整由随机数发生器产生的随机数在一个指定的范围内的方法叫比例缩整由随机数发生器产生的随机数在一个
16、指定的范围内的方法叫比例缩放放)To scale a random number as an integer value between 1 and N:1 + (int)rand() % NTo produce a random integer between the numbers a and b:a + (int)(rand() % (b - a + 1)Input/Output Library Functionsgetchar() can be used for single character inputinchar=getchar();scanf(“%c”,&inchar);putchar() expects a single character argument
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 六下信息技术《多任务程序》教学设计
- 如何合理规划每月预算计划
- 在线销售策略计划
- 学生学习目标与评价体系计划
- 节水宣传与教育活动计划
- 2025年单、双长链烷基甲基叔胺项目合作计划书
- 制定有效的危机应对预案计划
- 土地转包协议(2025年版)
- 2025年试验机械相关检测仪器项目发展计划
- 幼儿园动植物观察教育方案计划
- 质检员识图培训机械制图基础培训快速识图
- 组建代驾服务公司方案
- 鲁科版四年级下册英语全册教案设计18课时
- 尪痹的护理查房
- 新版现代西班牙语学生用书第一册课后习题答案
- 活动10《体验微视频拍摄乐趣》第二课时-体验微视频拍摄乐趣 第二课时 课件
- 浅谈物业管理行业工程造价控制
- 公文写作规范及技巧
- 创业指导(第二版)技工院校PPT完整全套教学课件
- 矩形的性质(公开课)
- 住房公积金补偿协议书
评论
0/150
提交评论