欢迎来到天天文库
浏览记录
ID:45034442
大小:658.50 KB
页数:77页
时间:2019-11-08
《C程序设计第七章函数》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Chapter7-FunctionsOutline7.1Introduction7.2ProgramModulesinC7.3MathLibraryFunctions7.4Functions7.5FunctionDefinitions7.6FunctionPrototypes7.7HeaderFiles7.8CallingFunctions:CallbyValueandCallbyReference7.9RandomNumberGeneration7.10Example:AGameofChance7.11StorageClasses7.12ScopeRules7.13Recursion7.1
2、4ExampleUsingRecursion:TheFibonacciSeries7.15Recursionvs.Iteration17.1IntroductionDivideandConquer——分而治之ConstructaprogramfromsmallerpiecesorcomponentsEachpiecemoremanageablethantheoriginalprogram函数是一段完成特定任务的程序。2模块化程序设计基本思想:将一个大的程序按功能分割成一些小模块特点:各模块相对独立、功能单一、结构清晰、接口简单控制了程序设计的复杂性提高元件的可靠性缩短开发周期避免程序开发的重
3、复劳动易于维护和功能扩充开发方法:自上向下,逐步分解,分而治之3C程序结构C是函数式语言必须有且只能有一个名为main的主函数C程序的执行总是从main函数开始,在main中结束函数不能嵌套定义,可以嵌套调用C是模块化程序设计语言47.2ProgramModulesinCFunctionsModulesinCProgramswrittenbycombininguser-definedfunctionswithlibraryfunctionsCstandardlibraryhasawidevarietyoffunctionsMakesprogrammer'sjobeasier-avoidrei
4、nventingthewheel57.2ProgramModulesinC(II)Functioncalls——函数调用Invokingfunctions——调用函数Providefunctionnameandarguments(data)FunctionperformsoperationsormanipulationsFunctionreturnsresultsBossasksworkertocompletetaskWorkergetsinformation,doestask,returnsresultInformationhiding:bossdoesnotknowdetails67.3
5、MathLibraryFunctionsMathlibraryfunctionsperformcommonmathematicalcalculations#includeFormatforcallingfunctionsFunctionName(argument);Ifmultiplearguments,usecomma-separatedlistprintf("%.2f",sqrt(900.0));Callsfunctionsqrt,whichreturnsthesquarerootofitsargumentAllmathfunctionsreturndatatypedou
6、bleArgumentsmaybeconstants,variables,orexpressions77.4FunctionsFunctionsModularizeaprogramAllvariablesdeclaredinsidefunctionsarelocalvariables(局部变量)KnownonlyinfunctiondefinedParametersCommunicateinformationbetweenfunctionsLocalvariablesBenefitsDivideandconquerManageableprogramdevelopmentSoftwarereu
7、sabilityUseexistingfunctionsasbuildingblocksfornewprogramsAbstraction-hideinternaldetails(libraryfunctions)Avoidscoderepetition87.5FunctionDefinitionsFunctiondefinitionformatreturn-value-typefunction-name(p
此文档下载收益归作者所有