--C语言程序设计课件PPT(英文)C-program-language-之5-Function-and-Program-Structure (1).ppt
- 文档编号:30861474
- 上传时间:2024-05-02
- 格式:PPT
- 页数:66
- 大小:1.32MB
--C语言程序设计课件PPT(英文)C-program-language-之5-Function-and-Program-Structure (1).ppt
《--C语言程序设计课件PPT(英文)C-program-language-之5-Function-and-Program-Structure (1).ppt》由会员分享,可在线阅读,更多相关《--C语言程序设计课件PPT(英文)C-program-language-之5-Function-and-Program-Structure (1).ppt(66页珍藏版)》请在冰豆网上搜索。
LOGOFunctionandProgramStructureChapterFourContentsBasicsofFunctionsBasicsofFunctions1returnstatementsreturnstatements2LocalvariablesandLocalvariablesandexternavariablesexternavariables3RecursionRecursion4ScopeRules5HeaderFilesHeaderFiles6StaticVariablesStaticVariables7RegisterVariablesRegisterVariables89TheCPreprocessorTheCPreprocessorFunctionandProgramStructure老师让几个同学编程,老师让几个同学编程,A负责编写求两个数的最大公约数;负责编写求两个数的最大公约数;B负责两个负责两个数的最小公倍数;数的最小公倍数;C负责判断这两个数是不是亲密数;负责判断这两个数是不是亲密数;D负责计算这两负责计算这两个数中有多少既能被个数中有多少既能被3整除又能被整除又能被7整除的数整除的数老师说出了两个数,老师说出了两个数,A,B,C,D立即把自己程序运行的结果告诉老师;立即把自己程序运行的结果告诉老师;老师又说了另外两个数,老师又说了另外两个数,A,B,C,D又给出新的结果又给出新的结果AABBCCDDmain()(inta,b;scanf(“%d,%d”,&a,&b);funa(a,b);funb(a,b);func(a,b);fund(a,b);36,120main()(inta,b;scanf(“%d,%d”,&a,&b);funa(a,b);funb(a,b);func(a,b);fund(a,b);36,120FunctionandProgramStructureAAmmain()ain()intinta,b,ha,b,h;sscanf(“%d,%d”,&a,&bcanf(“%d,%d”,&a,&b););if(aif(ab)b)a=a=a+ba+b;b=a-b;b=a-b;a=a-b;a=a-b;ffor(hor(h=a;ha;h=2;h-)=2;h-)IIf(f(a%ha%h=0&b%h=0)=0&b%h=0)bbreak;reak;pprintf(“%d,%d,%d”,a,b,hrintf(“%d,%d,%d”,a,b,h););funafuna(intinta,a,intintb)b)intinth;h;if(aif(ab)b)a=a=a+ba+b;b=a-b;b=a-b;a=a-b;a=a-b;ffor(hor(h=a;ha;h=2;h-)=2;h-)IIf(f(a%ha%h=0&b%h=0)=0&b%h=0)bbreak;reak;pprintf(“%d,%d,%d”,a,b,hrintf(“%d,%d,%d”,a,b,h););return;return;main()(inta,b;scanf(“%d,%d”,&a,&b);funa(a,b);funb(a,b);func(a,b);fund(a,b);36,120FunctionandProgramStructureCprogramsource1source2source3.sourcenTheCPreprocessorTheCPreprocessorfunction1function1function2function2.functiofunctionnnnFunctionisthesmallestunitinCprogram.Functiondefinitionscanappearinanyorder,andinonesourcefileorseveralAprogrammayresideinoneormoresourcefilesmoresourcefiles.Sourcefilesmaybecompiledseparatelycompiledseparatelyandloadedtogether,alongwithpreviouslycompiledfunctionsfromlibrariesFunctionDeclarationStatementExecutiveStatementVariablesFunctionprototypeVariablestypeVariablenameBlankstatementFunctioncallstatementControlstatementBlockstructureExpressionstatementFunctionandProgramStructureAprogramisjustasetofdefinitionsofvariablesandfunctions.Communicationbetweenthefunctionsisbyargumentsandvaluesreturnedbythefunctions,andthroughexternalvariables.DefinitionDefinitionPrototypePrototypeCallCallReturnReturnArgumentsArgumentsBasicsofFunctions1ThetypesoffunctionsTypeStandardStandardfunctionsfunctions(librarylibrary)Needuseheadfiles#includeUser-definedUser-definedfunctionsfunctionsEachfunctiondefinitionhastheform2Definitionofafunctionreturn-typefunction-name(argumentdeclarations)declarationsstatementsThetypeoftheresultthatthefunctionreturnsparametertypesandnamesIfreturntypeisomitted,intisassumedAminimalfunctionisemptyfunction,whichdoesnothingandreturnsnothing.BasicsofFunctionsfunc()printf(“*n”);main()inti;main()for(i=1;i4;i+)func();printf(“*n”);func();func();main()inti;for(i=1;iy?
x:
y;return(n);BasicsofFunctionsinput:
6.8,4.5m=?
thereisnofunctiondeclarationincaller.returntypeisfloat,theactualtypeofreturnvalueisint.floatm;%freturnstatement1returnstatementreturnexpression;Thereturnstatementisthemechanismforreturningavaluefromthecalledfunctiontoitscaller.Afunctionneedntreturnavalue,theexpressioncanbeomitted,ornoreturnstatement.Areturnstatementwithnoexpressionpassesanousefulvaluetothecaller.Sometimesitisatrouble.whendefiningfunctiontypeto“void”,noanyvaluereturned.Itisallowedthatseveralreturnstatementsoccurinafunction.voidfunctionname()floatf(floatx,floaty)floatz;z=x+y*y;if(!
y)return(0.0);return(cos(z);1.return(ab?
a:
b);2.return(0);3.return(x);4.retrun;5.noreturnstatement6.voidreturnstatementmain()inta;voiddisp();printf(“Enteraninteger:
n”);scanf(“%d”,&a);disp(a);Ifyiszero,return0Otherwisereturncos(z)voiddisp(intx)if(x0)printf(“Positive.n”);elseif(x=0)printf(“Zero.n”);elseprintf(“Negative.n”);5nesting-function.returnstatementwhilecallingafunction,anotherfunctioniscalled.fun1().fun2().fun3().fun1().fun2().nesting-functiondoesnotmeannesting-definition.voidbeijing();voidshanghai();voidtianjin();main()printf(“Iminmain.n”);beijing();printf(“Imfinallybackinmain().n”);voidbeijing()printf(“Iminbeijing.n”);shanghai();printf(“HereImbackinbeijing.n”);voidshanghai()printf(“NowIminshanghai.n”);tianjin();printf(“NowImbackinshanghai.n”);voidtianjin()printf(“Imintianjinnow.n”);mmmmain()ain()ain()ain()callcallfuncfuncA;A;A;A;funcfuncAAccallallfuncfuncBB;funcfuncBBreturnstatementLocalvariablesandexternalvariablesexternalLocalinnerFunctionBlockOutofintx,y;main()inta,b,c;.fun(a,b);.intfun(intm).shareLocalvariablesandexternalvariables1.localvariablesDeclaredwithinafunctionorablock.Canonlybeusedinthefunctionortheblockwhichdefinethem.Theyarecreatedaneweachtimethefunctioniscalled,anddestroyedonreturnfromthefunctionoroutsidetheblock.Theyarenotvisibleforotherfunctions.LocalvariablesindifferentfunctionscansharethesamenameTheformalargumentsaretreatedlikelocalvariablesfloatf(inta)intb,c;a,b,carelocalvariablesmain()intx=10;intx=20;printf(“%d“,x);printf(“%d“,x);Localvariablesandexternalvariablesmain()intx=1;voidf1(),f2();f1();f2(x);printf(“%cn”,x);voidf1()intx=3;printf(“%d”,x);voidf2(intx)printf(“%d”,+x);2.ExternalvariablesDefinedoutsideanyfunctionTheirvalueisretainedandisavailabletoanyotherfunctionwhichaccessesthem.CanbeinitializedonlyonceCansharethesamenamewithlocalvariable,butthelatterisusefulwithinthenamingfunction.shouldbedeclaredifuseitbeforeitsdefinition.Localvariablesandexternalvariablesexterninta;main()intI;for(I=1;I=10)thenoutputthelowestdigit;Nisdivided10N-N-1;callfunctionitself(N-N-1);elseoutputN;main()intx;printf(“nEnterN=”);scanf(“%d”,&x);int_turn(x);int_turn(n)intn;if(n=10)printf(“%d”,n%10);int_turn(n/10);elseprintf(“%d”,n);Recursionvoidmove(chargetone,charputone)printf(%c-%cn,getone,putone);voidhanoi(intn,charone,chartwo,charthree)if(n=1)move(one,three);elsehanoi(n-1,one,three,two);move(one,three);hanoi(n-1,two,one,three);main()intm;printf(Inputthenumberofdisks:
);scanf(%d,&m);printf(Thestepstomoving%3ddisks:
n,m);hanoi(m,A,B,C);HanoiproblemRecursionABCHanoiproblemScopeRulesHowaredeclarationswrittensothatvariablesareproperlydeclaredduringcompilation?
Howaredeclarationsarrangedsothatallthepieceswillbeproperlyconnectedwhentheprogramisloaded?
Howaredeclarationsorganizedsothereisonlyonecopy?
Howareexternalvariablesinitialized?
ScopeRulesexternalvariableDeclareinafunctionorblockCanonlybeusedinthefunctionorblockDeclareatanyplaceoutoffunctionlastsfromthepointatwhichitisdeclaredtotheendofthefilebeingcompiledLocalvariablesofthesamenameindifferentfunctionsareunrelatedIfanexternalvariableistobereferedtobeforeitisdefined,orifitisdefinedinadifferentsourcefilefromtheonewhereitisbeingused,thenanexterndeclarationismandatory.LocalvariableTheremustbeonlyonedefinitionofanexternalvariableamongallthefilesthatmakeupthesourceprogram;otherfilesmaycontainexterndeclarationstoaccessit.ScopeRulesexternalvariablesautovariablesregistervariablesstaticvariablesKeywordsexternautoregisterstaticThememorycanbedevidedseveralparts:
programStaticstorageDynamicstoragememoryCPUregisterStoreprogramStorestaticvariablesStoredynamicvariablesAccordingtothewayofstorage,wehave:
ScopeRulesKeywordsexternautoregisterstaticThedefaulttypeisautoScopeRulesThekey-word:
staticTwokinds:
staticexternalvariablestaticinternalvariableTheirvaluealwaysexistinmemory.main()inti;for(i=1;i=5;i+)f(i);f(intj)staticinta=100;autok=1;+k;printf(“%d+%d+%d=%dn”,a,k,j,a+k+j);a+=10;1.Staticinternalvariableinternalstaticvariablesprovideprivate,permanentstoragewithinasinglefunction.ScopeRulesDeclaredwithinafunctionCanonlybeusedinthefunctionwhichdefinethem.Theyarenotvisibleforotherfunctions.StaticvariablesindifferentfunctionscansharethesamenameTheyremaininexistenceratherthancomingandgoingeachtimethefunctionisactivated.staticinternalvariableintf(inta)autointb=0;staticintc=3;b+;c+;returna+b+c;main()intc,a=1;for(c=1;c3;c+)printf(“%dn”,f(a);ScopeRulesScopeRulesstaticexternalvariable2.StaticexternalvariableDeclaredoutsideanyfunctionLimitsthescopeofthatobjecttotherestofthesourcefilebeingcompiled.Externalstaticthusprovidesawaytohidenames,whichmustbeexternalsotheycanbeshared,yetwhichshouldnotbevisibletoothersfiles.CanonlybeusedinthefilewhichdefineitScopeRulesregistervariablesregistervariablesregisterregisterAregisterdeclarationadvisesthecompilerthatthevariableinquestionwillbeheavilyusedheavilyused.Thatregistervariablesaretobeplacedinmachineregistersplacedinmachineregisters.TheregisterdeclarationcanonlybeappliedtoautomaticvariablesandtotheformalparametersofafunctionOnlyafewvariablesineachfunctionmaybekeptinregisters,Onlycertaintypes(int,char)areallowed.ScopeRulesBlockStructureVariablescanbedefinedinablock-structuredfashionwithinafunctionintk=1;.fun(intm)m+=k;k+=m;chark=B;printf(“n%d”,k-A);printf(“%d,%d”,m,k);12Declarationsofvariables(includinginitializations)mayfollowtheleftbracethatintroducesanycompoundstatement.Variablesdeclaredinthiswayhideanyidenticallynamedvariablesinouterblocks,andremaininexistenceuntilthematchingrightbraceInitializationScopeRulesIntheabsenceofexplicitinitialization,externalandstaticvariablesareguaranteedtobeinitializedtozero.Automaticandregistervariableshaveundefined(i.e.,garbage)initialvalues.Forexternalandstaticvariables,theinitializermustbeaconstantexpression;theinitializationisdoneonce.Forautomaticandregistervariables,theinitializerisnotrestrictedtobeingaconstantmain()intx=1;voidprt(void);intx=3;prt();printf(“2ndx=%dn”,x);printf(“1stx=%dn”,x);voidprt(void)intx=5;printf(“3thx=%dn”,x);result:
3thx=52ndx=31stx=1x=1scopex=1scopex=3scopex=5scopeScopeRulesmain()voidincrement(void);increment();increment();increment();voidincrement(void)intx=0;x+;printf(“%dn”,x);result:
111main()voidincrement(void);increment();increment();increment();voidincrement(void)staticintx=0;x+;printf(“%dn”,x);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- -C语言程序设计课件PPT英文C-program-language-之5-Function-and-Program-Structure 1 语言程序设计 课件
链接地址:https://www.bdocx.com/doc/30861474.html