In C, you can ensure that:
1. All functions use value transfer: in fact, even a pointer is also a value transfer. In C ++, the reference is passed down to the end and the value is passed.
2. The passing process of parameters from real parameters to form parameters is actuallyInitialize (! = Value assignment) Process. When passing parameters, perform some arithmetic expressions and initialize them by values.
3. The scope of the parameter is the function field. It is the same as other automatic variables defined in the function and has no special characteristics.
4. When the function form parameter carries the const qualifier, the real parameter can be passed successfully. This transfer is equivalent to initialization. Once initialized, it cannot be changed.
5. The order in which parameters are transmitted is from right to left.
Example 1: (rule 2)
# Include <stdio. h>
VoidFunc (IntIi)
{
Printf ("In func (): % d \ n", Ii );
}
IntMain ()
{
IntI = 0;
Func (I ++ );
Printf ("Out func (): % d \ n", I );
Return0;
}
Result:
In func (): 0
Out func (): 1
It can be seen that when I is passed into the function parameter, it is the process of assigning values.
II = I ++;
Example 2: three small segmentsProgram(Rule 4)
# Include <stdio. h>
IntMain ()
{
IntI = 0;
Const IntJ = I;
Return0;
}
This program will not have any problems.
# Include <stdio. h>
IntMain ()
{
IntI = 0;
Const IntJ;
J = I;
Return0;
}
This program may be compiled incorrectly! So you must distinguishInitializationAndAssignmentRelationship
# Include <stdio. h>
VoidFunc (Const IntIi)
{
Printf ("In func (): % d \ n", Ii );
}
IntMain ()
{
IntI = 0;
Func (I );
Return0;
}
This program has no errors.
Example 3: (Rule 5)
# Include <stdio. h>
VoidFunc (IntI,IntJ)
{
Printf ("In func (): I = % d \ n", I );
Printf ("In func (): J = % d \ n", J );
}
IntMain ()
{
IntI = 0;
Func (I, I ++ );
Return0;
}
Result:
In func (): I = 1
In func (): J = 0
# Include <stdio. h>
VoidFunc (IntI,IntJ)
{
Printf ("In func (): I = % d \ n", I );
Printf ("In func (): J = % d \ n", J );
}
IntMain ()
{
IntI = 0;
Func (I, ++ I );
Return0;
}
Result:
In func (): I = 1
In func (): j = 1