C语言编程学习课件 (36).pdf
Programming In CProgramming In C The essence of calling function A C program can be composed of a main()function and several functions,and the tasks are completed by mutual calls between functions.Parameters are the main channel for data exchange between functions.Therefore,a thorough understanding of the parameters in a function and a grasp of the mechanism of parameter transfer are not only a prerequisite for the correct design and skillful application of functions,but also the key to program design and development.The essence of calling function The essence of a parameterized function call is to replace the formal parameters with actual parameters.Parameters are used to pass messages between different code blocks of a program.The actual parameters used in a function call must be exactly the same as the formal parameters in order,number,and data type.Programming In CProgramming In C#include stdio.h float f(float x,float y)float s;s=2*x*x+3*y+8;return s;void main()float a=1.2,b=3,result;result=f(a,b);printf(%6.2fn,result);(2)x=1.2 y=3 s=19.88(1)funtion be called x,y,s be created in memory Procedure of calling function formal parameter workingformal parameter working the function body be runningthe function body be running return return s (3)result=19.88 E.g.4:solvtion of f(x,y)=2x2+3y+8 Programming In CProgramming In C 1.pass by value Value passing takes constants,variables and expressions as actual parameters and passes values to the corresponding formal parameters.The actual parameter value is copied to the formal parameter which is completely isolated from the place that the information came from.So,any change of the formal parameter is not related to the actual parameter.Therefore,value passing is one-way.Array names,function names,pointers,etc.Can also be passed as actual parameters to formal parameters,in which case addresses are passed instead of values.Address passing is two-way.result E.g.2:one-way value passing#include stdio.h main()int a=2,b=3;swap(a,b);printf(“(2):a=%d,b=%dn,a,b);(1):a=3,b=2 (2):a=2,b=3 Function parameter passing 2.pass by address void swap(int a,int b)int t;t=a,a=b,b=a;printf(“(1):a=%d,b=%dn,a,b);Programming In CProgramming In C Error message error C2081:b:name in formal parameter list illegal int fun(int a,b)VC+compiler detect to arguments of function 1.Errors in formal parameter definition.Analysis of Error:When the parameter data types are the same,the data type description of the following parameters is omitted when defining the parameters.In this example,there is no data type specification for parameter.When compiling,the compiler gives an error message that parameter b is illegal.Programming In CProgramming In C VC+compiler detect to arguments of function Error message error C2198:fun:too few actual parameters#include stdio.h int fun(int a,int b)return a+b;main()printf(%dn,fun(4);2.The number of actual parameters is inconsistent with that of formal parameters.Analysis of Error:The number of actual argument must be the same as the formal parameter.Programming In CProgramming In C#include stdio.h int fun(int a,int b)return a+b;main()printf(%dn,fun(4.5,3);Warning information warning C4244:function:conversion from const double to int,possible loss of data VC+compiler detect to arguments of function 3.The data type of the actual parameter is different from that of the formal parameter.Analysis of Error:When the data type of the actual parameter is different from that of the formal parameter,the compiler will force the data type of the actual parameter to the data type of the formal parameter if the user ignores the warning,which may result in data loss.If the warning is ignored,the run result is 7 Programming In CProgramming In C VC+compiler detect to arguments of function result 7#include stdio.h int fun(int a,int b,int c)return a+b;main()printf(“%dn”,fun(4,3,5);4.The formal parameters are redundant.Analysis of Error:If this happens to a local variable in a function body,the compiler will give a warning that the variable is not referenced.for example:x:unreferenced local variable However,the compiler will not give a warning for the redundancy of formal parameters.Programming In CProgramming In C