




如果您无法下载资料,请参考说明:
1、部分资料下载需要金币,请确保您的账户上有足够的金币
2、已购买过的文档,再次下载不重复扣费
3、资料包下载后请先用软件解压,在使用对应软件打开
04Function (函数)Structure:KnowledgepointCisamodularprogramminglanguage(C是模块化程序设计语言)UnderstandingofthefunctionClassificationoffunctions(函数分类)FunctionDefinition(函数定义)FunctionDefinition(函数定义)合法标识符FunctionDefinitionFormat:returnexpression; return(expression); return; Annotation:Theprogramreturnstothecallingfunction,thevalueofexpressionreturnstoitscaller. 返回调用函数 Theremaybemorethanonereturnstatements,butthereisonlyonereturnvalueinafunction. 可能不止一个return语句,但一次只能带回一个返回值。 ReturnstatementPrototypingaFunction(函数的声明)Example: floatadd(float,float);/*functiondeclaration*/ main() { floata,b,c; scanf("%f,%f",&a,&b); c=add(a,b); printf("sumis%f",c); } floatadd(floatx,floaty) {floatz; z=x+y; return(z); }main() {floata,b; intc; scanf("%f,%f",&a,&b); c=max(a,b); printf("Maxis%d\n",c); } max(floatx,floaty) {floatz; z=x>y?x:y; return(z); }CallingaFunction(函数调用)CallingaFunction(函数调用)CallingaFunction(函数调用)CallingFunctionparametersandtheirtransfermode Formalparameter&ActualArguments 形式参数:定义函数时函数名后面括号中的变量名 实际参数:调用函数时函数名后面括号中的表达式说明: 实参必须有确定的值 形参必须指定类型 形参与实参类型一致,个数相同 若形参与实参类型不一致,自动按形参类型转换———函数调用转换 形参在函数被调用前不占内存;函数调用时为形参分配内存;调用结束,内存释放 例计算x的立方Parameterpassing Valueispassed Whenthefunctioncall,Allocationunitfortheparameter,andtheargumentvalueiscopiedintotheparameter;Endofthecall,parameterunitisreleased,Argumentstoretaintheoriginalvalue. Features: Theparameterandargumentstakedifferentinthememoryunit One-waytransfer参数传递方式 值传递方式方式:函数调用时,为形参分配单元,并将实参的值复制到形参中;调用结束,形参单元被释放,实参单元仍保留并维持原值 特点: 形参与实参占用不同的内存单元 单向传递7Addressispassed Functioncall,thedatastorageaddresspassedtotheparameter Features: Theparameterandargumentstakeinthesamestorageunit "Two-way"transfer Actualandformalparametersmustbeconstantorvariableaddress地址传递 方式:函数调用时,将数据的存储地址作为参数传递给形参 特点: 形参与实参占用同样的存储单元 “双向”传递 实参和形参必须是地址常量或变量/*ch9_3.c*/ swap(p1,p2) int*p1,*p2; {intp; p=*p1; *p1=*p2; *p2=p; } main() {inta,b; scanf("%d,%d",&a,&b); printf(“a=%d,b=%d\n”,a,b); printf(“swapped:\n”); swap(&a,&b); printf(”a=%d,b=%d\n",a,b); }Nestedfunctioncalls (函数嵌套调用)

ys****39
实名认证
内容提供者


最近下载