您好,欢迎访问三七文档
Chapter9User-DefinedFunctionsPROGRAMMINGINANSIC23/28/2020ModularProgramminginCBefore,thestructureofaCprogramweprogrammedis:CprogramProgramfilemain()DeclarationpartExecutablepart33/28/2020ModularProgramminginCGenerally,thestructureofCprogramis:CprogramFilemfunctionfaDeclarationpartExecutablepartFile1File2……Filen……functionf1functionf2……functionfb……43/28/2020ModularProgramminginC-ASimpleExampleWriteaprogramtoprintthefollowingoutputs:print_star(){printf(**************\n);}print_message(){printf(Howdoyoudo!\n);}main(){print_star();print_message();print_star();}**************Howdoyoudo!**************main(){printf(**************\n);printf(Howdoyoudo!\n);printf(**************\n);}53/28/2020ModularProgramminginC-ClassificationofFunctionsFromtheperspectiveofuser:Libraryfunctions:printf(),scanf(),sin(),strlen()...User-Definedfunctions:print_star()...Fromtheperspectiveoftheformoffunctions:Functionswithparameters:printf(Hello),sin(x)...Functionswithoutparameter:getchar(),print_star()...63/28/2020ModularProgramminginC-ClassificationofFunctionsFromtheperspectiveoftypeoffunctions:Voidfunctions:voidmain()...Integerfunctions:printf(),scanf()...Doublefunctions:sqrt(x),sin(x)...Characterfunctions:getchar()...……73/28/2020intmax(intx,inty){intz;z=xy?x:y;return(z);}FunctionDefinitionGeneralformofafunctiondefinition:function_typefunction_name(parameterlist){DeclarationpartExecutablepart}Ifitisnotexplicitlyspecified,thefunctionwillreturnanintvalue.Ifthefunctiondon'treturnanyvalue,itshouldspecifythereturntypeasvoid.Valididentifier.NoticetoavoidduplicatinglibraryfunctionnamesorOScommands.FormalparameterslistDeclarevariablesreceivingthedatatransferredbythecallingfunction.Itcanbevoid.max(intx,inty){intz;z=xy?x:y;return(z);}max(intx,y){intz;z=xy?x:y;return(z);}83/28/2020print_message(){printf(Hello!\n);}FunctionDefinitionGeneralformofafunctiondefinition:function_typefunction_name(parameterlist){DeclarationpartExecutablepart}FormalparameterslistDeclarevariablesreceivingthedatatransferredbythecallingfunction.Itcanbevoid.print_message(void){printf(Hello!\n);}Thisfunctionshouldreturnanintvalue.However,itdoesn'tusereturnstatementtoreturnanyvalue.Insuchcase,itwillreturnanuncertainintvalue.93/28/2020print_message(){printf(Hello!\n);}main(){printf(%d,print_message());}FunctionDefinitionGeneralformofafunctiondefinition:Hello!7print_message(){printf(Hello!\n);return(10);}main(){printf(%d,print_message());}Hello!10103/28/2020voidprint_message(){printf(Hello!\n);}main(){printf(%d,print_message());}FunctionDefinitionGeneralformofafunctiondefinition:void:thefunctiondoesn'treturnanyvalue.Error…7:Notanallowedtypeinfunctionmainvoidprint_message(){printf(Hello!\n);}main(){print_message();}Pvoidprint_message(){printf(Hello!\n);return(10);}main(){print_message();}Warning…3:Voidfunctionsmaynotreturnavaluein…113/28/2020FunctionParametersmain(){inta,b,sum;a=10;b=24;sum=add(a,b);printf(sum=%d\n,sum);}intadd(intx,inty){intz;z=x+y;return(z);}calladd(10,24)return:z=34sum=34main(){inta,b,sum;a=10;b=24;sum=add(a,b);printf(sum=%d\n,sum);}intadd(intx,inty){intz;z=x+y;return(z);}xandyareformalparameters•Definedinfunctionheaderoffunctiondefinition•Whendefined,wemustdeclaretheirtyperespectively•Beforecalled,theydon'trequestmemory.•Aftercalled,theyarereleased.aandbareactualparameters•Theymaybevariablesorconstantsorexpressions(formalparametersmustbevariables).•Actualparametersmusthaveassuredvalue.•Whencalling,actualparameterstransfertheirvaluestoformalparameters.Suchtransferisunidirectional.•Thetypesofactualparametersandformalparametersshouldbeconsistent.main(){inta,b,sum;a=10;b=24;sum=add(a,b);printf(sum=%d\n,sum);}intadd(inta,intb){intz;z=a+b;return(z);}main(){inta,b,sum;a=10;b=24;sum=add(a+b,15);printf(sum=%d\n,sum);}intadd(inta+b,intb){intz;z=a+b;return(z);}Actualparameterandformalparametercanusethesamename.Actualparameterscanbevariablesorconstantsorexpressions,butformalparametersmustbevariables.P123/28/2020FunctionParametersmain(){inta,b,sum;a=10;b=24;sum=add(a,b);printf(sum=%d\n,sum);}intadd(intx,inty){x=x+y;return(x);}Actualparameterstransfervaluestoformalparameters,butformalparameterscan'tinfluenceactualparameters.a=10,b=24x=10,y=24x=34return34sum=34ba××××sum××1024yx××××10243434133/28/2020ReturnValuesFunctionsmaysendbackavaluetothecallingfunction,throughthereturnstatement.Function:sendbackthecontrol(maybewithonevalue)tothecallingfucntion.return(expression);orreturnexpression;orreturn;143/28/2020ReturnValuesFunctionsmaysendbackavaluetothecallingfunction,throughthereturnstatement.return(expression);orreturnexpression;orreturn;intmax(intx,inty){intz;z=xy?x:y;return(z);}intmax(intx,inty){intz;z=xy?x:y;returnz;}intmax(intx,inty){returnxy?x:y;}153/28/2020ReturnValuesIndefinition,thetypeofthereturnvalueshouldbespecified.Ifitisnotexplicitlyspecified,thereturnvaluedefaultsasinttype.charletter(charch)floatsum(floatx,floaty)intmax(intx,inty)min(intx,inty)Thetypeofthereturnvalueshouldbeconsistentwiththetypespecifiedinthefunctionheader.163/28/20
本文标题:programming in ANSI C-Chapter 9 User-Defined Funct
链接地址:https://www.777doc.com/doc-4605666 .html