CodeAssign a value to a parameter.Replace this parameter with a temporary variable.
|
IntDiscount (IntInputval,IntQuantity,IntYeartodate)
{
If(Inputval> 50)
{
Inputval-= 2;
}
}
|
|
IntDiscount (IntInputval,IntQuantity,IntYeartodate)
{
IntResult = inputval;
If(Inputval> 50)
{
Result-= 2;
}
}
|
Motivation: Be clear about the meaning of "assign values to parameters. If you pass an object named Foo as a parameter to a function, "assign a value to the parameter" means to change foo so that it references another object. If you perform any operations on the "passed-in object", there is no problem. Here we will only discuss the situation where "foo is changed and directed to another object.
|
Void amethod (Object Foo)
{
Foo. modifyinsomeway ();
Foo = anotherobject;
} |
This method reduces the Definition of the code, and uses two parameter passing Methods: pass by value and pass by reference.
When passing by value, any modification to the parameter will not affect the call end. Those who have used the transfer by reference method may be confused at this point.
Another confusing place is the function itself. If you only use parameters to indicate "passed in ". The code will be much clearer, because this usage shows the same semantics in all languages.
Practice: 1. Create a temporary variable and assign the value of the parameter to be processed.
2With "value assignment to parameters" as the field, replace all subsequent reference points for this parameter with "reference to this temporary variable ".
3Modify the value assignment statement to assign values to new temporary variables.
4, Compilation, and testing. If the semantics of the Code is passed by reference, check whether this parameter is used after the call, you also need to check how many parameters passed by reference are assigned and then called. Try to return only one value in the rteturn mode. If you need to use more than one value, you can see if you can convert the large pile of data to be returned into a single object, or simply design a corresponding independent function for each returned value.