资源描述:
《C语言英文课件七函数.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Chapter7Functions(函数)7.1FoundationofFunctions7.2NestedCallingandRecursiveCalling7.3ArraysQuaParametersofFunctions7.4TheScopeofVariables7.5TheStorageTypeofVariables7.6LocalFunctionsandExternalFunctionsForeward[ex.7.1]ComputetheCombinationmain(){intm,n,i;longfact,c;scanf("%d%d",&m,&n);fact=1;for(i
2、=2;i<=m;i++)fact*=i;c=fact;fact=1;for(i=2;i<=n;i++)fact*=i;c=c/fact;fact=1;for(i=2;i<=m-n;i++)fact*=i;c=c/fact;printf("c=%d",c);}Designafunction/*defineafunctionfact*/longfact(intk){inti;longy=1;for(i=1;i<=k;i++)y*=i;return(y);}/*factiscalledbymain()*/main(){intm,n;longc;scanf("%d%d",&m,&n);c=
3、fact(m);/*callingfact*/c=c/fact(n);c=c/fact(m-n);printf("c=%d",c);}c=fact(m)/(fact(n)*fact(m-n));formalparameter(形参)kactualparameters(实参)m,n,m-nBasicconceptfunctiondeclarations(函数定义);formalparameters(形式参数);thevaluethefunctionreturns(函数返回值);theresult-typeofthefunction(函数类型);functioncall(函数的调用);
4、actualparameters(实在参数);parameterpassing(参数的传递);……7.1FoundationofFunctionsDECLARATIONSOFFUNCTIONS/*functionfact*/longfact(intk){inti;longy=1;for(i=1;i<=k;i++)y*=i;return(y);}1.FivefactorsoffunctionsFunctionnameDeclarationofarguments(nameandtypeofarguments)TypeoffunctionvalueFunctionReturningvalue
5、offunction2.Aprogramisjustasetofdefinitionsofvariablesandfunctions.mainabcdfghfeegfEachfunctiondefintionhastheformresult-typefunction-name(argument-specifiers){declarationsandstatements}Variouspartsmaybeabsent;aminimalfunctionisdummy(){}SortsofFunctionsbyuselibraryfunctions(库函数)functionsdefinedb
6、yuser(用户自定义函数)byformatfunctionstakeargumentsordon’ttakearguments(有参函数、无参函数)nullfunctions(空函数)functionsreturenresultordon’treturenresult(有返回值、无返回值)voidprintline(void){inti;for(i=1;i<=30;i++)printf("-");printf("");}longfact(intk){inti;longy=1;for(i=1;i<=k;i++)y*=i;return(y);}main(){intm,n;longc;
7、clrscr();printline();printf("m,n=");scanf("%d%d",&m,&n);c=fact(m)/(fact(n)*fact(m-n));printline();printf("c=%d",c);getch();}Givefollowingfunctions’type[ex.7.2]Defineafunctiontofindthemaxoneoftwonumbersmain(){intmax(intn1,i