程序设计(I)_ch04Functions.ppt

  1. 1、本文档共37页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
程序设计(I)_ch04Functions

Chapter 4 - Functions 4.1 Program Modules in C Functions Modules in C Programs combine user-defined functions with library functions C standard library 标准库has a wide variety of functions Function calls 函数调用 Invoking functions Provide function name and arguments (data)实际参数 (数据) Function performs operations or manipulations Function returns results Function call analogy: Boss asks worker to complete task Worker gets information, does task, returns result Information hiding: boss does not know details 从函数声明的角度看: 标准函数(库函数):由C++语言的集成 开发环境提供,存放在库(Library) 函数 中,用户在链接相应的库函数头文 件后便可以直接使用。 用户自定义的函数 从函数的形式看: 无参函数:调用该函数时,主调函数不需 函数 要将数据传递给被调用函数,只执 行指定的一组操作 有参函数:主调需传递数据给被调函数 Example Format for calling functions FunctionName( argument ); If multiple arguments, use comma-separated list printf( %.2f, sqrt( 900.0 ) ); Calls function sqrt, which returns the square root of its argument All math functions return data type double Arguments may be constants, variables, or expressions Math library functions 数学库函数 perform common mathematical calculations #include math.h 4.2 Functions Functions Modularize a program程序模块化 All variables declared inside functions are local variables局部变量 Known only in function defined Parameters形式参数(简称形参) Communicate information between functions Local variables局部变量 Benefits of functions Divide and conquer分而治之 Manageable program development Software reusability软件重用 Use existing functions as building blocks for new programs Abstraction - hide internal details (library functions库函数) Avoid code repetition避免代码重复 Function Definitions Function definition format return-value-type function-name( parameter-list ) { declarations and statements } Function-name: any valid identifier Return-value-type: data type of the result (default int) void – indicates that the function returns nothing Parameter-list形式参数表: comma separated list, declares parameters A type must be listed explicitly for each parameter unless, the parameter is of type int

文档评论(0)

xy88118 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档