C语言学习第五章(英文版)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Chapter5FunctionsSchoolofSoftware,NanchangHangKongUniversityChapter5FunctionsChapter5FunctionsSchoolofSoftware,NanchangHangKongUniversity5.1Introduction5.2ProgramModulesinC5.3MathLibraryFunctions5.4Functions5.5FunctionDefinitions5.6FunctionPrototypes5.7CallingFunctions:CallbyValue5.8StorageClasses5.9ScopeRules5.10Recursion5.11ExampleUsingRecursion:TheFibonacciSeries5.12Recursionvs.IterationOutlineChapter5FunctionsSchoolofSoftware,NanchangHangKongUniversityFunctionDefinitionsFunctionPrototypesCallingFunctions:CallbyValueStorageClassesScopeRulesRecursionRecursionvs.IterationKeyPointsChapter5FunctionsSchoolofSoftware,NanchangHangKongUniversityWhatisafunctionAfunctionisaself-containedblockofstatementsthatperformatask.EveryCprogramcanbethoughtofasacollectionofthesefunctions.5.1IntroductionChapter5FunctionsSchoolofSoftware,NanchangHangKongUniversity5.1IntroductionDivideandconquerConstructaprogramfromsmallerpiecesorcomponentsThesesmallerpiecesarecalledmodulesEachpiecemoremanageablethantheoriginalprogramChapter5FunctionsSchoolofSoftware,NanchangHangKongUniversity5.1IntroductionAgroupofdeclarationsandstatementsthatisassignedanameeffectively,anamedstatementblockusuallyhasavalueAsub-programwhenwewriteourprogramwealwaysdefineafunctionnamedmaininsidemainwecancallotherfunctionswhichcanthemselvesuseotherfunctions,andsoon…Chapter5FunctionsSchoolofSoftware,NanchangHangKongUniversity5.1Introduction#includestdio.hvoidmessage();voidmain(){message();printf(“\ncry,andyoustopthemonotony!”);}voidmessate(){printf(“\nSmile,andtheworldsmileswithyou…”);}Chapter5FunctionsSchoolofSoftware,NanchangHangKongUniversity5.2ProgramModulesinCFunctionsModulesinCProgramscombineuser-definedfunctionswithlibraryfunctionsCstandardlibraryhasawidevarietyoffunctionsFunctioncallsInvokingfunctionsProvidefunctionnameandarguments(data)FunctionperformsoperationsormanipulationsFunctionreturnsresultsFunctioncallanalogy:BossasksworkertocompletetaskWorkergetsinformation,doestask,returnsresultInformationhiding:bossdoesnotknowdetailsChapter5FunctionsSchoolofSoftware,NanchangHangKongUniversity5.2ProgramModulesinC--Example-Squareintmain(void){doublenum=0.0,sqr=0.0;printf(enteranumber\n);scanf(%lf,&num);sqr=square(num);printf(squareof%gis%g\n,num,sqr);return0;}Thisisafunctiondefinedoutsidemaindoublesquare(doublea){returna*a;}HereiswherewecallthefunctionsquareChapter5FunctionsSchoolofSoftware,NanchangHangKongUniversity5.2ProgramModulesinCWhyusefunctionsWritingfunctionsavoidsrewritingthesamecodeoverandover.printfandscanfaregoodexamples…Breakyourproblemdownintosmallersub-taskseasiertosolvecomplexproblemsTheymakeaprogrammucheasiertoreadandmaintain.Chapter5FunctionsSchoolofSoftware,NanchangHangKongUniversity5.3MathLibraryFunctionsMathlibraryfunctionsAcollectionofmathematicalfunctionsperformcommonmathematicalcalculationsNeedtoincludetheheaderfilemath.h#includemath.hFormatforcallingfunctionsFunctionName(argument);Ifmultiplearguments,usecomma-separatedlistprintf(%.2f,sqrt(900.0));Callsfunctionsqrt,whichreturnsthesquarerootofitsargumentAllmathfunctionsreturndatatypedoubleArgumentsmaybeconstants,variables,orexpressionsChapter5FunctionsSchoolofSoftware,NanchangHangKongUniversity5.3MathLibraryFunctionsMathfunctionssin(x),cos(x),tan(x)asin(x),acos(x),atan(x)xisgiveninradianslog(x)sqrt(x)pow(x,y)–raisextotheythpower.ceil(x),floor(x)…andmoreChapter5FunctionsSchoolofSoftware,NanchangHangKongUniversity5.4FunctionsFunctionsModularizeaprogramAllvariablesdeclaredinsidefunctionsarelocalvariablesKnownonlyinfunctiondefinedParametersCommunicateinformationbetweenfunctionsLocalvariablesBenefitsoffunctionsDivideandconquerManageableprogramdevelopmentSoftwarereusabilityUseexistingfunctionsasbuildingblocksfornewprogramsAbstraction-hideinternaldetails(libraryfunctions)AvoidcoderepetitionChapter5FunctionsSchoolofSoftware,NanchangHangKongUniversityAccordingtothefunctiondefinition,isitreasonable?dummy(){}Aboveistheminimalfunction,whichdoesnothingandreturnnothing.Sometimes,ado-nothingfunctionlikethisisusefulasaplaceholder(keeper)duringprogramdevelopment.5.4FunctionsChapter5FunctionsSchoolofSoftware,NanchangHangKongUniversity5.5FunctionDefinitionsFunctiondefinitionformatreturn-value-typefunction-name(parameter-list){local-declarationsstatementsreturnreturn-value;}Examples:doublesquare(doublea){returna*a;}intmain(void){…}Chapter5FunctionsSchoolofSoftware,NanchangHangKongUniversity5.5FunctionDefinitionsFunctiondefinitionformat(continued)return-value-typefunction-name(parameter-list){local-declarationsstatementsreturnreturn-value;}Function-name:anyvalididentifierReturn-value-type:datatypeoftheresult(defaultint)void–indicatesthatthefunctionreturnsnothingParameter-list:commaseparatedlist,declaresparametersAtypemustbelistedexplicitlyforeachparameterunless,theparameterisoftypeintChapter5FunctionsSchoolofSoftware,NanchangHangKongUniversity5.5FunctionDefinitionsFunctiondefinitionformat(continued)return-value-ty

1 / 94
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功